【问题标题】:WPF MultiBinding Converter Object TypeWPF MultiBinding 转换器对象类型
【发布时间】:2017-09-05 03:06:21
【问题描述】:

我已将复选框的Checked 事件绑定到方法。我还通过转换器将两个命令参数传递给该方法(实现IMultiValueConverter)。在下面的代码中,两个CommandParameter 绑定的类型分别为boolstring

<CheckBox Name="cbTopLeftHeader" Content="Top Left Header"> 
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Checked">
                <i:InvokeCommandAction Command="{Binding IncludeCheckedCommand}">
                    <i:InvokeCommandAction.CommandParameter>
                        <MultiBinding Converter="{StaticResource MultiBindingConverter}" ConverterParameter="IncludeChecked">
                            <Binding ElementName="cbTopLeftHeader" Path="IsChecked"></Binding>
                            <Binding Source="{StaticResource TopLeft}"></Binding>
                        </MultiBinding>
                    </i:InvokeCommandAction.CommandParameter>
                </i:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
</CheckBox>

如果我换行,为什么会这样:

<Binding ElementName="cbTopLeftHeader" Path="IsChecked"></Binding>

与:

<Binding RelativeSource="{RelativeSource Self}" Path="IsChecked"></Binding>

在我的转换器中,IsChecked 参数从 bool 类型更改为 DependencyProperty (DependencyProperty.UnsetValue) 类型?

如何在不通过名称将元素绑定到自身的情况下传递Checked 属性?

【问题讨论】:

  • 您没有绑定 CheckBox 的 CommandParamter 属性(例如 here),因此 RelativeSource Self 不是 CheckBox。

标签: c# wpf multibinding relativesource


【解决方案1】:

去掉交互触发器,使用CheckBoxCommandCommandParameter属性:

<CheckBox Name="cbTopLeftHeader" Content="Top Left Header" Command="{Binding IncludeCheckedCommand}">
    <CheckBox.CommandParameter>
        <MultiBinding Converter="{StaticResource MultiBindingConverter}" ConverterParameter="IncludeChecked">
            <Binding RelativeSource="{RelativeSource Self}" Path="IsChecked"/>
            <Binding Source="{StaticResource TopLeft}"></Binding>
        </MultiBinding>
    </CheckBox.CommandParameter>
</CheckBox>

或者坚持使用ElementName 绑定到CheckBox 的当前方法。 {RelativeSource Self}InvokeCommandAction 不是 CheckBox

【讨论】:

  • 谢谢mm8。我没有意识到(虽然现在看起来很明显)复选框的命令默认是Checked 事件。您的回答和上面的 Clemens 评论回答了我的问题。
猜你喜欢
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多