如果只有一个项目,如何隐藏组合框切换按钮? [英] How to hide combobox toggle button if there is only one item?

查看:240
本文介绍了如果只有一个项目,如何隐藏组合框切换按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序。在一个窗口中有一个组合框。如果只有一个项目,我想隐藏切换按钮并禁用组合框。

I have a WPF application. In one window there is a combobox..and I want to hide the toggle button and disable the combo box if there is only one item.

我将如何实现?

我试过下面的代码隐藏切换按钮。但是没有运气

I have tried the below code for hiding the toggle button. But of no luck

任何帮助将不胜感激。感谢

Any help would be appreciated. thanks

<ComboBox x:Name="CList" ItemsSource="{Binding Path=C}"  >                    
    <Style TargetType="{x:Type ToggleButton}" >
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=Items.Count, ElementName=CList}" Value="1">
                <Setter Property="Visibility" Value="Hidden" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ComboBox>


推荐答案

更好的解决方案是替换组合框与项目计数为零时的控制模板(只包含文本块)。

The better solution is to replace the template of combo box with a control template(which only contain textblock) when the item count is zero.

这里是同样的xaml。

Here is the xaml for the same.

<ComboBox Name="CList" ItemsSource="{Binding Path=C}" 
                     SelectedItem="{Binding Path=CC}" VerticalAlignment="Center" Margin="0,0,10,0" >
                    <ComboBox.Style>
                        <Style TargetType="{x:Type ComboBox}" >
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=Items.Count, ElementName=CList}" Value="1">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate>
                                                <TextBlock Text="{Binding Items[0], ElementName=CList}" />
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ComboBox.Style>
                </ComboBox>

这篇关于如果只有一个项目,如何隐藏组合框切换按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆