I have a situation where I need to style the selected item in the ComboBox in different ways (make the text bold), when it is one of all but one value. For example, in the dropdown "What is your favorite primary color?" I would have four options: No Preference , Red , Green and Blue . ComboBox elements are just text with a default style, images, or something else unusual, as well as C # classes not related to ComboBoxItems.
When the user specifies a preference from the list, I want to highlight this choice by setting the text of the selected item in the collapsed list so that it is bold. If the user selects No Preference , the font weight should remain normal.
I got a 90% solution by setting the FontWeight property of a ComboBox to Bold in a style with a DataTrigger defined as SelectedItem != No Preference . However, it stylizes all the items in the ComboBox item list, including everything in the drop-down list. I would like these elements to always display with normal font weight.
Is it possible?
Edit
I am trying to use the @crazyarabian method to style ComboBoxItem with MultiTrigger. Style Definition:
<Style x:Key="SelectedItemStyle"> <Setter Property="ComboBoxItem.FontWeight" Value="Normal" /> <Style.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="ComboBoxItem.IsSelected" Value="True" /> <Condition Binding="{Binding IsNoPreferenceSelected,Mode=OneWay}" Value="False" /> </MultiTrigger.Conditions> <Setter Property="ComboBoxItem.FontWeight" Value="Bold" /> </MultiTrigger> </Style.Triggers> </Style>
and applies to ComboBox in the following data table:
<DataTemplate x:Key="PrimaryColoursTemplate" DataType="{x:Type ViewModels:PrimaryColoursViewModel}"> <ComboBox ItemsSource="{Binding PrimaryColours}" SelectedItem="{Binding SelectedPrimaryColour}" ItemContainerStyle="{StaticResource SelectedItemStyle}" /> </DataTemplate>
Unfortunately, this kills WPF:
System.Windows.Data Error: 8 : Cannot save value from target back to source. BindingExpression:Path=IsDropDownOpen; DataItem='ComboBox' (Name=''); target element is 'ToggleButton' (Name=''); target property is 'IsChecked' (type 'Nullable`1') InvalidOperationException:'System.InvalidOperationException: Must have non-null value for 'Property'.
The application dies with a NullReferenceException , which is thrown after the InvalidOperationException above (or, possibly, leads to it, I can not decrypt the output). The only thing I can think of that could be the reason for this is to resolve the property in the binding in my second MultiTrigger state, but I don't get any binding errors at all. Here's the top of the stack trace in case this helps too:
InvalidOperationException:'System.InvalidOperationException: Must have non-null value for 'Property'. at System.Windows.Condition.Seal(ValueLookupType type) at System.Windows.ConditionCollection.Seal(ValueLookupType type) at System.Windows.MultiTrigger.Seal() at System.Windows.TriggerCollection.Seal() at System.Windows.Style.Seal() at System.Windows.StyleHelper.UpdateStyleCache(FrameworkElement fe, FrameworkContentElement fce, Style oldStyle, Style newStyle, Style& styleCache) at System.Windows.FrameworkElement.OnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args) at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType) at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal) at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) at System.Windows.Controls.ItemsControl.ApplyItemContainerStyle(DependencyObject container, Object item)