Home > Enterprise >  WPF CommandParameter value from code behind
WPF CommandParameter value from code behind

Time:09-30

How to bind a variable from code behind to CommandParameter?

Snippet from WPF tree view MyTreeView.xaml:

<TreeView.ContextMenu>
    <ContextMenu>
        <MenuItem Header="save items" Command="CommandInMvvmModel" CommandParameter="ParameterInCodeBehind"/>
    </ContextMenu>
</TreeView.ContextMenu>

ParameterInCodeBehind is a variable in MyTreeView.xaml.cs

CodePudding user response:

You can Set CommandParameter from code behing via giving name of that MenuItem.

<TreeView.ContextMenu>
    <ContextMenu>
        <MenuItem x:Name="menu" Header="save items" Command="CommandInMvvmModel" CommandParameter="ParameterInCodeBehind"/>
    </ContextMenu>
</TreeView.ContextMenu>

Code behind :

menu.CommandParameter = "value"
  • Related