0

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

1
  • In case it is a public property of the MyTreeView class: CommandParameter="{Binding ParameterInCodeBehind, RelativeSource={RelativeSource AncestorType=MyTreeView}}" Commented Sep 29, 2021 at 7:42

1 Answer 1

2

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.