[Just reaslised this was an old question ... I'll leave the answer/solution for anyone that finds this and requires a solution]
If I understand your question correctly, you want to set the
FontWeight of
ComboBoxItem
in the Dropdown list in Edit Mode to
Bold if item is "group".
To do this, you need to create a style for the
ComboBoxItem
. You can not do this directly on the column itself, but you can as a
Resource
for the
DataGrid
or at I higher level depending on the specificity that you require. Blow is a working example of how to do this:
1. CodeBehind with data:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public List<string> Choices {get; set;} = new()
{
"Choice 1",
"Choice 2",
"Choice 3"
};
public ObservableCollection<Person> Items { get; set; } = new()
{
new() { Name = "Freddie", Age = 21, Choice="Choice 1" },
new() { Name = "Milly", Age = 18, Choice="Choice 2" },
new() { Name = "Caddie", Age = 23, Choice="Choice 3" },
};
private void Button_Click(object sender, RoutedEventArgs e)
{
Items.RemoveAt(0);
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Choice { get; set; }
}
2. The View:
<Window x:Class="WpfDataGridComboBoxStyling.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfDataGridComboBoxStyling"
mc:Ignorable="d" x:Name="Window1"
Title="MainWindow" Height="450" Width="800">
<Grid DataContext="{Binding ElementName=Window1}">
<Grid.Resources>
<CollectionViewSource x:Key="ChoicesCVS"
Source="{Binding Choices}" />
</Grid.Resources>
<DataGrid AutoGenerateColumns="False"
ItemsSource="{Binding Items}">
<DataGrid.Resources>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Margin" Value="8 2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<TextBlock Text="{Binding}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Text" Value="Choice 2">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Foreground" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Age" Binding="{Binding Age}" />
<DataGridComboBoxColumn Header="Mode" Width="SizeToHeader"
SelectedItemBinding="{Binding Choice}"
ItemsSource="{Binding Source={StaticResource ChoicesCVS}}">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="Foreground" Value="Red" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
NOTE: I have not implemented the full template for the
ComboBoxItem
so as to keep it focused on the solution. You can find the full template here:
ComboBox Styles and Templates - Microsoft Docs[
^]
Hope this helps!