如果只有一个项目,如何隐藏组合框切换按钮?

9

我有一个WPF应用程序,在其中一个窗口中有一个下拉框,如果只有一个选项,我想隐藏切换按钮和禁用下拉框。

如何实现?

我尝试了以下代码来隐藏切换按钮,但没有成功。

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

<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>
3个回答

10
更好的解决方案是,在组合框项数为零时,用一个控件模板(仅包含TextBlock)替换组合框的模板。
以下是相应的XAML代码。
<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>

我也使用了这个解决方案,它很好用,所以谢谢你。我知道这是一个旧帖子,但是否有一种方法可以在列表中的一个项目更改时更新文本框中的值? - mobearette
如果您已经为组合框创建了模板,则此方法将无法正常工作。 - Randall Deetz
@RandallDeetz 和已经设置了模板的所有人,你可以在样式中(TargetType后面)添加 BasedOn="{StaticResource {x:Type ComboBox}}"。这对我来说解决了问题。 - IDarkCoder

4
您需要更改ComboBox模板并在其中实现触发器。您无法从外部访问模板中的控件。

(您可以复制并修改现有模板, 直接修改模板的一部分几乎是不可能的)


0
你也可以随时使用转换器:
(抱歉我没有完全阅读您的问题)

转换器

using System;
using System.Windows;
using System.Windows.Data;
using System.Globalization;

namespace WPFSandbox
{
    public class ComboBoxItemCountToEnabledConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value.GetType() == typeof(Int32))
            {
                if ((int)value > 1)
                    return true;
            }

            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ComboBoxItemCountToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && value.GetType() == typeof(Int32))
            {
                if ((int)value > 1)
                    return Visibility.Visible;
            }

            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

XAML

<Window 
... 
...
xmlns:converters="clr-namespace:WPFSandbox">

<Window.Resources>
    <converters:ComboBoxItemCountToVisibilityConverter x:Key="ComboBoxItemCountToVisibilityConverter"/>
    <converters:ComboBoxItemCountToEnabledConverter x:Key="ComboBoxItemCountToEnabledConverter"/>
</Window.Resources>

<StackPanel>
   <ComboBox ItemsSource="{Binding C}" IsEnabled="{Binding Path=C.Count, Converter={StaticResource ComboBoxItemCountToEnabledConverter}}"/>
   <ToggleButton Visibility="{Binding Path=C.Count, Converter={StaticResource ComboBoxItemCountToVisibilityConverter}}"/>
</StackPanel>

我想隐藏切换按钮,而不是完全隐藏组合框。 - Relativity

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接