ba126
ba126

Reputation: 109

How do I make the WPF ComboBox items visible, but non-selectable?

I am trying to make the items of my WPF comboBox visible (preferably greyed out) but not allow the user to select them using c# code.

I have tried the following:

comboBoxName.IsHitTestVisible = false comboBoxName.Focusable = false

However, this prevents them seeing the contents of the comboBox all together. How would I make the contents visible but cannot be selected in c#?

Upvotes: 1

Views: 1415

Answers (1)

thatguy
thatguy

Reputation: 22089

Set an item container style that will disable each ComboBoxItem.

<ComboBox x:Name="comboBoxName" ItemsSource="{Binding Collection}">
   <ComboBox.ItemContainerStyle>
      <Style TargetType="{x:Type ComboBoxItem}">
         <Setter Property="IsEnabled" Value="False"/>
      </Style>
   </ComboBox.ItemContainerStyle>
</ComboBox>

If you use code-behind, can define and apply the item container style like this.

var itemContainerStyle = new Style(typeof(ComboBoxItem));
var isEnabledSetter = new Setter(IsEnabledProperty, false);

itemContainerStyle.Setters.Add(isEnabledSetter);
comboBoxName.ItemContainerStyle = itemContainerStyle;

Upvotes: 2

Related Questions