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


Solved the mystery!
I don't really know why, but InputBinding.CommandParameterProperty is only available with .NET framework 4.0. I was working with 3.0, so I was unable to bind the CommandParameter in the code.
If anyone knows how to get around this it would be very helpful.



http://bit.ly/12Lb47v

+2


source







All Articles