9

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.

How would I achieve this ?

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>
H.B.
  • 166,899
  • 29
  • 327
  • 400
Relativity
  • 6,690
  • 22
  • 78
  • 128

3 Answers3

10

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.

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>
Relativity
  • 6,690
  • 22
  • 78
  • 128
  • I used this solution too, it worked fine, so thank you. I know this is an old post, but is there a way to update the value in the text box when the one item in the list changes? – mobearette Jun 25 '14 at 07:53
  • This doesn't work if you've already created a template for your combo box. – Randall Deetz Dec 18 '15 at 18:12
  • @RandallDeetz and everyone that already set a template, you can add `BasedOn="{StaticResource {x:Type ComboBox}}"` to the style (behind TargetType). That fixed it for me – IDarkCoder Oct 18 '18 at 11:10
4

You would need to change the Template of the ComboBox and implement the trigger inside that. You have no access to the controls in the template from the outside.

(You could copy and modify the existing template, directly modifying a part of the template is practically impossible)

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
0

You can always use a Converter also:
(Sorry I didn't 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>
MyKuLLSKI
  • 5,285
  • 3
  • 20
  • 39