2

如果没有项目,我正在尝试创建一个触发器以禁用组合框下拉按钮。这是迄今为止我尝试过的 XAML 代码,但是我不确定如何检测 ComboBox 中是否不包含任何项目,以及如何禁用专门下拉列表的按钮。

<Style TargetType="ComboBox">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ComboBox}">
                        <ControlTemplate.Triggers>
                            <Trigger Property="Items.Count" Value="0">

                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
4

1 回答 1

1

这对我有用:

<ComboBox IsEnabled="{Binding RelativeSource=
   {RelativeSource Mode=Self}, Path=ItemsSource.Count}"/>

假设您绑定到 ItemsSource 属性的任何内容都有一个 count 方法(它适用于 ObservableCollection)。在 xaml 中 count 为 0 解析为 false 实际上有点有趣,但是在 C# 中情况并非如此。

如果需要以编程方式将其添加到控件中,可以将其添加到样式中

<Style TargetType="ComboBox" x:Key="ComboStyle">
    <Setter Property="IsEnabled" Value="{Binding RelativeSource=
       {RelativeSource Mode=Self}, Path=ItemsSource.Count}"/>
</Style>

ComboBox cbo = new ComboBox();
cbo.ItemsSource = MyData;
cbo.Style = Resources["ComboStyle"] as Style;
于 2013-03-19T22:08:32.037 回答