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?

Was it helpful?

Solution

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.

https://msdn.microsoft.com/it-it/library/system.windows.input.inputbinding.commandparameterproperty(v=vs.100).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top