wpf - 使用 CommandParameters 和 MultiBindings?

标签 wpf binding mvvm mvvm-light wpfdatagrid

是否可以在多重绑定(bind)中使用 CommandParameter="{Binding}"?
我正在尝试在数据网格中执行此操作。

<CheckBox.CommandParameter>
    <MultiBinding Converter="{StaticResource CDetailConverter}">
        <Binding Path ="IsChecked" ElementName="chkSelection"/>                                        
        <Binding ConverterParameter="{Binding}"/>
    </MultiBinding>
</CheckBox.CommandParameter>

第二个 Binding 引发错误。

最佳答案

简而言之,答案是否定的。

在你的第二个内心 Binding你设置了ConverterParameter .这有几个问题:

一、Binding是独立于 MultiBinding 的自己的类与 ConverterConverterParameter特性。在这里你设置了ConverterParameter属性而不设置 Converter属性(property)。请记住 ConverterParameter传递给 Binding's指定的转换器,无论它是否在 MultiBinding 中使用或不。如果您要添加 Converter在这里,转换器将传递指定的 ConverterParameter .

您可能打算做的是设置 ConverterParameter在外 MultiBinding它也有这个属性:

<CheckBox.CommandParameter>
    <MultiBinding Converter="{StaticResource CDetailConverter}" ConverterParameter="{Binding }">
        <Binding Path ="IsChecked" ElementName="chkSelection"/>                                        
    </MultiBinding>
</CheckBox.CommandParameter>

如果你试试这个,你会很快看到 ConverterParameter不能成为 Binding 的目标表达式,因为它不是 DependencyProperty .

由于您无法绑定(bind)到CommandParameter ,典型的解决方法是修改您的 IMultiConverter接受附加值,并通过绑定(bind)表达式提供此值:
<CheckBox.CommandParameter>
    <!-- CDetailConverter updated to expect an additional value in the values array -->
    <MultiBinding Converter="{StaticResource CDetailConverter}">
        <Binding Path ="IsChecked" ElementName="chkSelection"/>
        <Binding />                                   
    </MultiBinding>
</CheckBox.CommandParameter>

希望这可以帮助!

关于wpf - 使用 CommandParameters 和 MultiBindings?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8290411/

相关文章:

wpf - 如何在wpf中检查三个控件中的任何一个是否同时具有焦点?

c# - 枚举上的 DataTrigger 作为 WPF 样式上的触发器值

c# - 无法将 TwoWay 模式设置为 x :Bind

android - 使用MVVM在RecyclerView的网格和线性之间切换的更好方法是什么?

c# - 使用 SetBinding 的 Xamarin.Forms 绑定(bind)不起作用

c# - 无法访问 Telerik 中的 Sort 属性 :RadComboBox

c# - 如何将 WPF 窗口发送到后面?

xaml - 绑定(bind)到 ElementName 取决于绑定(bind)顺序

silverlight - 在 Silverlight 和 MVVM 中动态添加 View

wpf - ControlTemplate 或 DataTemplate 中的自定义资源字典