是否可以在参考资料中声明MultiBindning并在控件中使用它?

时间:2013-01-14 11:58:39

标签: .net wpf multibinding

以下是我想在资源部分

中使用的代码段
 <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>

它不允许这样做并在使用中说无效类型

1 个答案:

答案 0 :(得分:2)

错误信息非常清楚:

  

无法在类型的'Resources'属性上设置'MultiBinding'   “主窗口”。 'MultiBinding'只能在DependencyProperty上设置   DependencyObject。

然而,您可以在Style for ComboBoxes中声明绑定:

<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}"/>

当然不一定要将它放入Style中。您也可以直接将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>