How to hide combobox toggle button if there is only one item?

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

How can i achieve this?

I tried the code below to hide the toggle button. But no luck

Any help would be greatly 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> 
+8
visibility wpf xaml combobox togglebutton
source share
3 answers

The best solution is to replace the combo box template with a control template (which contains only a text block) when the number of elements is zero.

Here is haml 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> 
+8
source share

You will need to modify the Template ComboBox and implement a trigger in it. You do not have access to the controls in the template outside.

(You can copy and modify an existing template, directly changing part of the template is almost impossible)

+4
source share

You can also use the converter:
(Sorry, I did not fully read your question)

Converters

 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> 
0
source share

All Articles