WPF sets binding on CommandParameter dynamically in code
I need to dynamically create some TreeViewItems, each one must have a Command binding to a DoubleClick Mouse Action. The problem is, I want to pass a parameter to this command, but I don't know how to do it.
Currently the code is:
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 like this:
CommandParameter="{Binding Path=SelectedItem, ElementName=MainTV}"
How to set dynamically in code?
+3
source to share
1 answer