Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I conditionally style only the text in the selected item of a ComboBox?
    text
    copied!<p>I have a situation where I need to style the selected item in a ComboBox differently (make the text bold) when it is one of all except one value. For example, in the drop-down box labelled "What is your favourite primary colour?" I would have four options: <code>No Preference</code>, <code>Red</code>, <code>Green</code>, and <code>Blue</code>. The ComboBox items are just text with default styling, no images or anything else fancy, and are C# classes, not wrapped in ComboBoxItems. </p> <p>When the user specifies a preference from the list, I want to highlight that choice by setting the text of the selected item <em>in the collapsed list</em> to be bold. If the user chooses <code>No Preference</code>, the font weight should remain normal. </p> <p>I have achieved a 90% solution by setting the FontWeight property on the ComboBox to Bold in a Style with a DataTrigger defined as <code>SelectedItem != No Preference</code>. However, this styles all items in the ComboBox's list of items, including all those in the drop-down list. I would like those items to <em>always</em> be displayed with a normal font weight. </p> <p>Is this possible?</p> <p><strong>Edit</strong></p> <p>I have been trying @crazyarabian's method of styling the ComboBoxItem with a MultiTrigger. The style definition is:</p> <pre><code>&lt;Style x:Key="SelectedItemStyle"&gt; &lt;Setter Property="ComboBoxItem.FontWeight" Value="Normal" /&gt; &lt;Style.Triggers&gt; &lt;MultiTrigger&gt; &lt;MultiTrigger.Conditions&gt; &lt;Condition Property="ComboBoxItem.IsSelected" Value="True" /&gt; &lt;Condition Binding="{Binding IsNoPreferenceSelected,Mode=OneWay}" Value="False" /&gt; &lt;/MultiTrigger.Conditions&gt; &lt;Setter Property="ComboBoxItem.FontWeight" Value="Bold" /&gt; &lt;/MultiTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <p>and it is applied to a ComboBox in the following DataTemplate:</p> <pre><code>&lt;DataTemplate x:Key="PrimaryColoursTemplate" DataType="{x:Type ViewModels:PrimaryColoursViewModel}"&gt; &lt;ComboBox ItemsSource="{Binding PrimaryColours}" SelectedItem="{Binding SelectedPrimaryColour}" ItemContainerStyle="{StaticResource SelectedItemStyle}" /&gt; &lt;/DataTemplate&gt; </code></pre> <p>Unfortunately, this kills WPF: </p> <pre><code>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'. </code></pre> <p>The application dies with a <code>NullReferenceException</code>, which is thrown after the InvalidOperationException above (or perhaps leads to it, I can't decipher the output). The only thing I can think of that might be causing this is resolving the property in the binding in my second MultiTrigger condition, but I don't get any binding errors at all. Here's the top of the stack trace in case that helps too:</p> <pre><code>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&amp; 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&amp; 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) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload