Search code examples
wpfxamlcomboboxisenabled

WPF ComboBox IsEnabled from Items Count


Im wanting the XAML of a ComboBox to handle its own IsEnabled property based on whether or not it has Items. If the DataTable in the code behind returns Items, id like the ComboBox to be enabled otherwise if no Items are added, it remains or becomes a disabled control. Is this at all possible?

My current ComboBox setup:

<ComboBox x:Name="ImportDate" 
        DisplayMemberPath="FileDate" 
        SelectedValuePath="ID" 
        ItemsSource="{Binding Mode=OneWay}" 
        SelectedIndex="0" 
        Style="{DynamicResource sanComboBox_Standard}" />

Solution

  • If you want the ComboBox to be enabled when it has items, and disabled when there are no items, you could just bind IsEnabled to the HasItems property:

    <ComboBox x:Name="ImportDate" 
        DisplayMemberPath="FileDate" 
        SelectedValuePath="ID" 
        ItemsSource="{Binding Mode=OneWay}" 
        SelectedIndex="0" 
        Style="{DynamicResource sanComboBox_Standard}"
        IsEnabled="{Binding HasItems, RelativeSource={RelativeSource Self}}" />