是否可以在资源中声明多绑定并在控件中使用它?



这是我想在资源部分使用的代码片段

 <UserControl.Resources> 
  <MultiBinding Converter="{StaticResource ResourceKey=EnableConference}" 
                  x:Key="EnableifConferenceIsNotNullAndIsStarted">
        <Binding Path="SelectedConference" Mode="OneWay"/>
        <Binding Path="SelectedConference.ConferenceStatus" Mode="OneWay"/>
  </MultiBinding>
</UserControl.Resources>

,我想在下面的

控件中使用它
<ComboBox><ComboBox.IsEnabled><StaticResource ResourceKey="EnableifConferenceIsNotNullAndIsStarted"></ComboBox.IsEnabled></ComboBox>

在使用

时,它不允许这样做,并且表示为无效类型。

错误信息非常清楚:

'MultiBinding'不能在type的'Resources'属性上设置"主窗口"。"MultiBinding"只能在DependencyProperty上设置

但是你可以在ComboBoxes的Style中声明绑定:

<Style TargetType="ComboBox" x:Key="MyComboBoxStyle">
    <Setter Property="IsEnabled">
        <Setter.Value>
            <MultiBinding Converter="{StaticResource ResourceKey=EnableConference}">
                <Binding Path="SelectedConference" Mode="OneWay"/>
                <Binding Path="SelectedConference.ConferenceStatus" Mode="OneWay"/>
            </MultiBinding>
        </Setter.Value>
    </Setter>
</Style>

并在合适的地方使用:

<ComboBox Style="{StaticResource MyComboBoxStyle}"/>

当然不一定要把这个放到样式中。你也可以直接将MultiBinding赋值给ComboBox的IsEnabled属性:

<ComboBox>
    <ComboBox.IsEnabled>
        <MultiBinding Converter="{StaticResource ResourceKey=EnableConference}">
            <Binding Path="SelectedConference" Mode="OneWay"/>
            <Binding Path="SelectedConference.ConferenceStatus" Mode="OneWay"/>
        </MultiBinding>
    </ComboBox.IsEnabled>
</ComboBox>

最新更新