Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 668
Relativity Avatar asked Jan 16 '12 23:01

Relativity


2 Answers

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>
like image 116
Relativity Avatar answered Nov 13 '22 09:11

Relativity


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)

like image 29
H.B. Avatar answered Nov 13 '22 08:11

H.B.