Click here to Skip to main content
15,851,335 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
hi everyone.
I have a datagrid with items binding to a field.

I don't know how display specific items in bold.
I use a Converter={StaticResource combogroupitem} that's return "Normal" or "Bold"


Any idea?


XML
  <DataGridComboBoxColumn Width="200"
  Header="Description"
                                           
  ItemsSource="{Binding Source={StaticResource inventory_desc_priceViewSource}}"
  SelectedValueBinding="{Binding FK_Inventory, Mode=TwoWay, 
  UpdateSourceTrigger=PropertyChanged}"
  SelectedValuePath="PK_Inventory"
  DisplayMemberPath="Description">

  <DataGridComboBoxColumn.EditingElementStyle>
  <Style TargetType="{x:Type ComboBox}">
  <EventSetter Event="Selector.SelectionChanged" Handler="SomeSelectionChanged" />

                                       
<Setter Property= "ItemsControl. FontWeight"  Value="{Binding Is_group,Source={StaticResource inventory_desc_priceViewSource}, Converter={StaticResource combogroupitem}}"/>
                        
  </Style>

</DataGridComboBoxColumn.EditingElementStyle>

</DataGridComboBoxColumn>


What I have tried:

I can't use EditingControlShowing because WPF datagrid doesn't have that implementation.
Posted
Updated 20-Sep-22 10:34am
v2
Comments
#realJSOP 10-Jul-21 8:02am    
Does specifically setting it to "bold" work?
Hector Cheva 12-Jul-21 8:17am    
the combobox use two types of items (item or group).
example (two items and two groups)
#item1 ----> NORMAL
#item2 ----> NORMAL
#group1 ----> BOLD
#group2 ----> BOLD
Adérito Silva 31-Jul-21 16:12pm    
I'm not sure how to address the question. For example, what is the exact error and where it occurs?

FontWeight uses FontWeight type. If you return a string in your converter, it won't work, because you would need to convert the string to FontWeight (using FontWeightConverter, for instance). Is that the error you get?

Also, DataGrid has several styles that can be set, including for groups (GroupStyle property). Are these the groups you mean?
Hector Cheva 5-Aug-21 4:37am    
Isn't a error. What do I need is differentiate group of items in one only combobox contained in a Datagrid as a DatagridComboboxI mean; I have items that has to be normal and others items in the same combo has to be bold, because they came from different databases
Adérito Silva 31-Jul-21 16:20pm    
To style items differently, you can use CellTemplate property on each column. You would set it with a DataTemplate and use DataTriggers and/or DataType to specify what items should use as template and, for example, set its font weight. In this case, if your items have a property like 'IsGroup', you would use a DataTrigger to set the font weight. If groups and items are of different types, you would use the DataType property of DataTemplate to restrict the template to that specific type.

1 solution

[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:
C#
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:
XML
<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!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900