Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is no need to get into anything as despicable as owner-draw-- we are talking about <em>WPF</em> here, not WinForms. In WinForms, your only solution was to write more code. In WPF, we can solve this problem with a few very simple custom templates. For this example, I used <a href="http://kaxaml.com/" rel="noreferrer">Kaxaml</a>, a free light-weight XAML editor. No code-behind was required. <strong>Kaxaml</strong> comes packed with a bunch of "starter" styles called <em>Simple Styles</em>. I used the <em>ComboBox Simple Style</em> and made modifications from that. So although this looks like a lot of XAML, I really just started with the boilerplate one and added a couple lines. </p> <p>You can probably think of more elegant ways of triggering the font weight change; I used <code>SelectedIndex</code>.</p> <pre><code>&lt;Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;Page.Resources&gt; &lt;DataTemplate x:Key="SelectionBoxTextTemplate"&gt; &lt;TextBlock FontWeight="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}, Path=FontWeight}" Text="{Binding}"/&gt; &lt;/DataTemplate&gt; &lt;ControlTemplate x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}"&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition/&gt; &lt;ColumnDefinition Width="20"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Border x:Name="Border" Grid.ColumnSpan="2" Background="#C0C0C0" BorderBrush="#404040" BorderThickness="1" CornerRadius="2"/&gt; &lt;Border Grid.Column="0" Margin="1" Background="#FFFFFF" BorderBrush="#404040" BorderThickness="0,0,1,0" CornerRadius="2,0,0,2"/&gt; &lt;Path x:Name="Arrow" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z" Fill="#404040"/&gt; &lt;/Grid&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="ToggleButton.IsMouseOver" Value="true"&gt; &lt;Setter TargetName="Border" Property="Background" Value="#808080"/&gt; &lt;/Trigger&gt; &lt;Trigger Property="ToggleButton.IsChecked" Value="true"&gt; &lt;Setter TargetName="Border" Property="Background" Value="#E0E0E0"/&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsEnabled" Value="False"&gt; &lt;Setter TargetName="Border" Property="Background" Value="#EEEEEE"/&gt; &lt;Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA"/&gt; &lt;Setter Property="Foreground" Value="#888888"/&gt; &lt;Setter TargetName="Arrow" Property="Fill" Value="#888888"/&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;Style x:Key="{x:Type ComboBox}" TargetType="{x:Type ComboBox}"&gt; &lt;Setter Property="SnapsToDevicePixels" Value="true"/&gt; &lt;Setter Property="OverridesDefaultStyle" Value="true"/&gt; &lt;Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/&gt; &lt;Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/&gt; &lt;Setter Property="ScrollViewer.CanContentScroll" Value="true"/&gt; &lt;Setter Property="MinWidth" Value="120"/&gt; &lt;Setter Property="MinHeight" Value="20"/&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type ComboBox}"&gt; &lt;Grid&gt; &lt;ToggleButton Name="ToggleButton" Grid.Column="2" ClickMode="Press" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" Template="{StaticResource ComboBoxToggleButton}"&gt; &lt;/ToggleButton&gt; &lt;ContentPresenter Name="ContentSite" HorizontalAlignment="Left" Margin="3,3,23,3" VerticalAlignment="Center" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{StaticResource SelectionBoxTextTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" IsHitTestVisible="False"/&gt; &lt;TextBox x:Name="PART_EditableTextBox" HorizontalAlignment="Left" Margin="3,3,23,3" VerticalAlignment="Center" Background="Transparent" Focusable="False" IsReadOnly="{TemplateBinding IsReadOnly}" Style="{x:Null}" Visibility="Hidden"/&gt; &lt;Popup Name="Popup" AllowsTransparency="True" Focusable="False" IsOpen="{TemplateBinding IsDropDownOpen}" Placement="Bottom" PopupAnimation="Slide"&gt; &lt;Grid Name="DropDown" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" SnapsToDevicePixels="True"&gt; &lt;Border x:Name="DropDownBorder" Background="#FFFFFF" BorderBrush="#888888" BorderThickness="1"/&gt; &lt;ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True"&gt; &lt;StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained"/&gt; &lt;/ScrollViewer&gt; &lt;/Grid&gt; &lt;/Popup&gt; &lt;/Grid&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="HasItems" Value="false"&gt; &lt;Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsEnabled" Value="false"&gt; &lt;Setter Property="Foreground" Value="#888888"/&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsGrouping" Value="true"&gt; &lt;Setter Property="ScrollViewer.CanContentScroll" Value="false"/&gt; &lt;/Trigger&gt; &lt;Trigger Property="Popup.AllowsTransparency" SourceName="Popup" Value="true"&gt; &lt;Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/&gt; &lt;Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsEditable" Value="true"&gt; &lt;Setter Property="IsTabStop" Value="false"/&gt; &lt;Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/&gt; &lt;Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/&gt; &lt;/Trigger&gt; &lt;Trigger Property="SelectedIndex" Value="1"&gt; &lt;Setter Property="FontWeight" Value="Bold"/&gt; &lt;/Trigger&gt; &lt;Trigger Property="SelectedIndex" Value="2"&gt; &lt;Setter Property="FontWeight" Value="Bold"/&gt; &lt;/Trigger&gt; &lt;Trigger Property="SelectedIndex" Value="3"&gt; &lt;Setter Property="FontWeight" Value="Bold"/&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}"&gt; &lt;Setter Property="SnapsToDevicePixels" Value="true"/&gt; &lt;Setter Property="OverridesDefaultStyle" Value="true"/&gt; &lt;Setter Property="FontWeight" Value="Normal"/&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type ComboBoxItem}"&gt; &lt;Border Name="Border" Padding="2" SnapsToDevicePixels="true"&gt; &lt;ContentPresenter/&gt; &lt;/Border&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsHighlighted" Value="true"&gt; &lt;Setter TargetName="Border" Property="Background" Value="#DDDDDD"/&gt; &lt;/Trigger&gt; &lt;Trigger Property="IsEnabled" Value="false"&gt; &lt;Setter Property="Foreground" Value="#888888"/&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/Page.Resources&gt; &lt;StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"&gt; &lt;TextBlock Margin="5" Text="What is your favorite primary colour?"/&gt; &lt;ComboBox Width="150" SelectedIndex="0"&gt; &lt;ComboBoxItem&gt;No Preference&lt;/ComboBoxItem&gt; &lt;ComboBoxItem&gt;Red&lt;/ComboBoxItem&gt; &lt;ComboBoxItem&gt;Green&lt;/ComboBoxItem&gt; &lt;ComboBoxItem&gt;Blue&lt;/ComboBoxItem&gt; &lt;/ComboBox&gt; &lt;/StackPanel&gt; &lt;/Page&gt; </code></pre> <p>I used the <code>ContentTemplate</code> property of the <code>ContentPresenter</code> in <code>ComboBox</code> to add a custom data template (<code>SelectionBoxTextTemplate</code>). That <code>TextBlock</code> grabs its <code>FontWeight</code> from an ancestor combo-box. I then added a template for the individual items, that forces them to normal font weight. This got the result you were looking for:</p> <p><img src="https://i.stack.imgur.com/3EjzP.png" alt="enter image description here"></p>
 

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