【问题标题】:Using CommandParameters and MultiBindings?使用 CommandParameters 和 MultiBindings?
【发布时间】:2012-01-07 14:11:44
【问题描述】:

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

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

第二个Binding抛出错误。

【问题讨论】:

  • Converter 参数不是依赖属性,所以你不能Bind 给它一些东西....你想做什么??
  • Protip:不需要在标题中添加标签,如果没有实际的异常类型和消息,“抛出错误”实际上是没有用的。在这种情况下,您很幸运,多绑定命令参数的整个转换器要求是众所周知的。将来,它可能会导致您的问题得不到任何答案!

标签: wpf binding mvvm mvvm-light wpfdatagrid


【解决方案1】:

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

在您的第二个内部Binding 中,您设置了ConverterParameter。这有几个问题:

首先,Binding 是独立于 MultiBinding 的自己的类,具有 ConverterConverterParameter 属性。在这里,您设置了ConverterParameter 属性,而没有设置Converter 属性。请记住,ConverterParameter 被传递给Binding's 指定的转换器,无论它是否在MultiBinding 中使用。如果您要在此处添加Converter,则转换器将传递指定的ConverterParameter

您可能想要做的是在外部MultiBinding 上设置ConverterParameter,它也有这个属性:

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

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

由于您无法绑定到 CommandParameter,典型的解决方法是修改您的 IMultiConverter 以接受附加值,并通过绑定表达式提供此值:

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

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2017-07-05
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 2011-01-11
    • 1970-01-01
    • 2012-06-27
    相关资源
    最近更新 更多