c# - 如何使用样式和 DataTrigger 绑定(bind)到 XAML 中的项目源计数?

标签 c# wpf xaml

如果我按名称调用列表,我可以绑定(bind)到项目源,但是,我无法通过获取每个单独组合框中的项目计数来使绑定(bind)正常工作。

这是我想在 XAML 中执行的操作。我需要更改什么才能使此绑定(bind)正常工作?

<Grid.Resources>
  <Style TargetType="ComboBox">                             
     <Style.Triggers>
       <DataTrigger Binding="{Binding Path=Items.Count}" Value="0">
          <Setter Property="IsEnabled" Value="False"></Setter>
       </DataTrigger>
     </Style.Triggers>
   </Style>
</Grid.Resources>

   <ComboBox
    ItemsSource="{Binding MyList}"               
    SelectedItem="{Binding SelectedElement}"
    ItemTemplate="{StaticResource MyTemplate}">
      </ComboBox>

最佳答案

包括 RelativeSource绑定(bind)中的组件:

<DataTrigger Binding="{Binding Path=Items.Count, RelativeSource={RelativeSource Self}}" 
             Value="0"
             >

按照您当前的方式,绑定(bind)子系统将在您设置为 ComboBox 的 DataContext 的任何内容上查找 Items.Count 属性。

关于c# - 如何使用样式和 DataTrigger 绑定(bind)到 XAML 中的项目源计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25809394/

相关文章:

c# - FrameworkElement 的 VisualStateManager.GoToState

c# - WPF 图像停止重绘

c# - WPF 中 IP 地址的文本框验证

wpf - 是否可以在同一资源部分引用资源字典并定义样式?

c# - WpfAnimatedGif 不要停留在特定的框架中

c# - ServiceStack 客户端和服务器时间之间的差异

c# - 具有多个具有相同键的条目的字典

C#线程/锁混淆

c# - WPF 中的日历控件奇怪的行为

c# - Areo Glass Effect 和 windowStyle 设置为 none 会导致窗口调整大小无法正常运行