DataGridComboBoxColumn how to display multiple fields?

Dmtr_Grms 331 Reputation points
2022-11-03T14:57:15.397+00:00

I would like to display more than one filed in my DataGridComboBoxColumn as it is possible with a normal Combobox. For example I would like to display the Currency Code and its Currency description or an Account Code with its description in order to help the user to select the right code.
Thanks in advance for your answers.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,683 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
768 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 40,786 Reputation points Microsoft Vendor
    2022-11-04T08:21:54.373+00:00

    For binding multiple properties to DataGridComboBoxColumn, you could refer to the following soltution.

    Xaml:

           <DataGrid x:Name="dg" ItemsSource="{Binding MyDatas}" AutoGenerateColumns="False">  
                    <DataGrid.Columns>  
                        <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>  
                        <DataGridTextColumn Header="Num" Binding="{Binding Num}"/>  
                        <DataGridTemplateColumn Header="ComboBoxDatas">  
                            <DataGridTemplateColumn.CellTemplate>  
                                <DataTemplate>  
                                    <ComboBox SelectedValue="{Binding Index}" SelectedValuePath="Index" HorizontalContentAlignment="Stretch"   
                                              ItemsSource="{Binding ComboboxDatas}">  
                                        <ComboBox.ItemContainerStyle>  
                                            <Style TargetType="{x:Type ComboBoxItem}">  
                                                <Setter Property="Template">  
                                                    <Setter.Value>  
                                                        <ControlTemplate>  
                                                            <Grid>  
                                                                <Grid.ColumnDefinitions>  
                                                                    <ColumnDefinition Width="Auto"/>  
                                                                    <ColumnDefinition Width="*"/>  
                                                                    <ColumnDefinition Width="*"/>  
                                                                </Grid.ColumnDefinitions>  
                                                                <TextBlock Margin="5" Grid.Column="0" Text="{Binding Index}"/>  
                                                                <TextBlock Margin="5" Grid.Column="1" Text="{Binding Name}"/>  
                                                                <TextBlock Margin="5" Grid.Column="2" Text="{Binding Symbol}"/>  
                                                            </Grid>  
                                                        </ControlTemplate>  
                                                    </Setter.Value>  
                                                </Setter>  
                                            </Style>  
                                        </ComboBox.ItemContainerStyle>  
                                        <ComboBox.ItemTemplate>  
                                            <DataTemplate>  
                                                <Grid>  
                                                    <Grid.ColumnDefinitions>  
                                                        <ColumnDefinition Width="Auto"/>  
                                                        <ColumnDefinition Width="*"/>  
                                                        <ColumnDefinition Width="*"/>  
                                                    </Grid.ColumnDefinitions>  
          
                                                    <TextBlock Margin="5" Grid.Column="0" Text="{Binding Index}"/>  
                                                    <TextBlock Margin="5" Grid.Column="1" Text="{Binding Name}"/>  
                                                    <TextBlock Margin="5" Grid.Column="2" Text="{Binding Symbol}"/>  
                                                </Grid>  
                                            </DataTemplate>  
                                        </ComboBox.ItemTemplate>  
                                    </ComboBox>  
                                </DataTemplate>  
                            </DataGridTemplateColumn.CellTemplate>  
                        </DataGridTemplateColumn>  
                    </DataGrid.Columns>  
                </DataGrid>  
    

    Codebehind:

    257098-code.txt

    The result:
    257154-image.png

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Michael Taylor 48,976 Reputation points
    2022-11-03T18:29:25.327+00:00

    Yes you can do that. You didn't provide us how you're doing the binding but out of the box ToString is called to convert items to text to render them. If you are binding to a model then override the ToString method to display what you want. If you're binding to a property on the object instead then create a new property that exposes the combined values the way you want. Since you're talking about joining several properties (I assume) then I don't believe custom formatting would work well here. However if you simply wanted to change the formatting of an existing property then the bindings format string would solve that.