【问题标题】:WPF ComboBox IsEnabled from Items CountWPF ComboBox IsEnabled 从项目计数
【发布时间】:2018-04-23 13:37:57
【问题描述】:

我希望 ComboBox 的 XAML 根据其是否具有项来处理其自己的 IsEnabled 属性。如果后面代码中的DataTable返回Items,id就像要启用的ComboBox,否则如果没有添加Items,则保留或成为禁用控件。这是可能吗?

我当前的组合框设置:

<ComboBox x:Name="ImportDate" 
        DisplayMemberPath="FileDate" 
        SelectedValuePath="ID" 
        ItemsSource="{Binding Mode=OneWay}" 
        SelectedIndex="0" 
        Style="{DynamicResource sanComboBox_Standard}" />

【问题讨论】:

  • 请注意,设置Mode=OneWay 是多余的。 ItemsSource="{Binding}" 就足够了。

标签: wpf xaml combobox isenabled


【解决方案1】:

如果您希望ComboBox 在有项目时启用,在没有项目时禁用,您可以将IsEnabled 绑定到HasItems 属性:

<ComboBox x:Name="ImportDate" 
    DisplayMemberPath="FileDate" 
    SelectedValuePath="ID" 
    ItemsSource="{Binding Mode=OneWay}" 
    SelectedIndex="0" 
    Style="{DynamicResource sanComboBox_Standard}"
    IsEnabled="{Binding HasItems, RelativeSource={RelativeSource Self}}" />

【讨论】:

  • 知道怎么做就这么简单!谢谢。
  • 最后,有人发布了如何在没有行为和风格的情况下从逻辑上处理这个问题。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-06
  • 2013-07-14
  • 2016-01-17
  • 1970-01-01
相关资源
最近更新 更多