Solved the mystery!
I don't really know why, but InputBinding.CommandParameterProperty is available only since .NET framework 4.0. I was working with 3.0 so I was unable to bind the CommandParameter in code.
If anyone knows how to bypass this, it will be very helpful.
WPF Set Binding on CommandParameter dynamically in code
-
10-03-2022 - |
Question
I need to dynamically create some TreeViewItems, each of them needs to have a Command bind to DoubleClick Mouse Action. The problem is that i want to pass a parameter to this Command, but I don't know how to do it.
Currently code:
private void AddExecuted(object sender, ExecutedRoutedEventArgs e)
{
MyTreeViewItem T = new MyTreeViewItem();
InputBinding IB = new InputBinding(RenameCommand, new MouseGesture(MouseAction.LeftDoubleClick));
Binding B = new Binding("SelectedItem");
B.Source = MainTV;
//BindingOperations.SetBinding(IB, IB.CommandParameterProperty /*CommandParameterProperty does not exist*/, B);
T.InputBindings.Add(IB);
MainTV.Items.Add(T);
e.Handled = true;
}
I usually set in XAML in this way:
CommandParameter="{Binding Path=SelectedItem, ElementName=MainTV}"
How to set dynamically in code?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow