c# - WPF 在代码中动态设置 CommandParameter 上的绑定(bind)

标签 c# wpf dynamic binding commandparameter

我需要动态创建一些 TreeViewItems,它们中的每一个都需要有一个命令绑定(bind)到 DoubleClick 鼠标操作。问题是我想将参数传递给此命令,但我不知道该怎么做。

当前代码:

    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;
    }

我通常在XAML中这样设置:

    CommandParameter="{Binding Path=SelectedItem, ElementName=MainTV}"

如何在代码中动态设置?

最佳答案

解开了谜团!
我真的不知道为什么,但 InputBinding.CommandParameterProperty 仅在 .NET Framework 4.0 之后可用。我使用的是 3.0,因此无法在代码中绑定(bind) CommandParameter。
如果有人知道如何绕过这个,那将非常有帮助。

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

关于c# - WPF 在代码中动态设置 CommandParameter 上的绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14959672/

相关文章:

C++分配动态结构

python - Django 动态模型属性

c# - Xamarin 表格 : StackLayout inside RelativeLayout is not filling the whole screen

c# - 网格清理代码

c# - 使用脚本将 LinqPad 结果控制台导出为 Html 文件

c# - 商业 WPF 应用程序的映射

c# - 如果绑定(bind)不匹配,则将项目添加到 itemssource

ios - 如何使用 Autolayout 确保干净地堆叠多个动态 UIView

c# - 如何使用 Xamarin 更改 ActionBar 菜单项的 Action View

c# - 继承 ApiController 与 IHttpController 的区别