使用 CommandParameters 和多重绑定?

发布于 2024-12-18 13:02:09 字数 451 浏览 0 评论 0原文

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

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

第二个 Binding 抛出错误。

Is it possible to use CommandParameter="{Binding}" in a multi binding?
I am trying to do this in a data grid.

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

The second Binding throws an error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無處可尋 2024-12-25 13:02:09

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

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

首先,Binding 是它自己的类,与 MultiBinding 分开,同时具有 ConverterConverterParameter属性。此处您已设置 ConverterParameter 属性,但未设置 Converter 属性。请记住,ConverterParameter 会传递到 Binding 指定的转换器,无论它是否在 MultiBinding 中使用。如果您要在此处添加一个Converter,那么该转换器将被传递指定的ConverterParameter

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

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

如果您尝试这样做,您很快就会看到 ConverterParameter code> 不能是 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>

希望这会有所帮助!

In a nutshell, the answer is no.

In your second inner Binding you have set ConverterParameter. There are a couple of problems with this:

First, Binding is its own class separate from MultiBinding with both Converter and ConverterParameter properties. Here you have set the ConverterParameter property without setting the Converter property. Remember that ConverterParameter is passed to the Binding's specified converter regardless if it is used within a MultiBinding or not. If you were to add a Converter here, then the converter would be passed the specified ConverterParameter.

What you probably meant to do was set the ConverterParameter on the outer MultiBinding which also has this property:

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

If you try this, you will quickly see that ConverterParameter can not be the target of a Binding expression since it is not a DependencyProperty.

Since you can not bind to CommandParameter, the typical workaround is to modify your IMultiConverter to accept an additional value, and supply this value through a binding expression:

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

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文