Why is my DataGridComboBoxColumn not populated

David McCallum 51 Reputation points
2021-02-02T11:06:42.833+00:00

Given the XAML block below, the ComboBox named "Populated" shows the items define in the ItemSource when expanded, whereas whilst the DataGridComboBoxColum named "NotPopulated" uses the same ItemSource shows nothing when expanded. (See attached images)

Am I missing something?

<Grid>  
                    <Grid.RowDefinitions>  
                        <RowDefinition Height="*" />  
                        <RowDefinition Height="Auto" />  
                    </Grid.RowDefinitions>  
                    <DataGrid  
                        AutoGenerateColumns="False"  
                        ItemsSource="{Binding SiteModels}"  
                        Style="{StaticResource DefaultStyle}">  
                        <DataGrid.Columns>  
                            <DataGridTextColumn  
                                Binding="{Binding Label}"  
                                Header="Site Model"  
                                IsReadOnly="True" />  
                            <DataGridTextColumn  
                                Binding="{Binding Number}"  
                                Header="# Parts"  
                                IsReadOnly="True" />  
                            <DataGridComboBoxColumn  
                                x:Name="NotPopulated"  
                                Width="*"  
                                DisplayMemberPath="Label"  
                                Header="Pro Model"  
                                ItemsSource="{Binding ProModels}"  
                                SelectedItemBinding="{Binding SelectedItem}" />  
                        </DataGrid.Columns>  
                    </DataGrid>  
                    <ComboBox  
                        x:Name="Populated"  
                        Grid.Row="1"  
                        DisplayMemberPath="Label"  
                        ItemsSource="{Binding ProModels}" />  
                </Grid>  

62974-image-2.png62969-image-1.png

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,676 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.
765 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2021-02-02T14:34:20.857+00:00

    Hi,
    if you don't use static properties you can use StaticResource like in following demo with your code:

    XAML:

    <Window x:Class="Window075"  
            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:WpfApp1"  
            xmlns:vmns="clr-namespace:WpfApp1.WpfApp075"  
            mc:Ignorable="d"  
            Title="Window075" Height="450" Width="800">  
      <Window.Resources>  
        <vmns:ViewModel x:Key="vm"/>  
        <Style x:Key="DefaultStyle"/>  
      </Window.Resources>  
      <Grid DataContext="{StaticResource vm}">  
        <Grid>  
          <Grid.RowDefinitions>  
            <RowDefinition Height="*" />  
            <RowDefinition Height="Auto" />  
          </Grid.RowDefinitions>  
          <DataGrid  
             AutoGenerateColumns="False"  
             ItemsSource="{Binding SiteModels}"  
             Style="{StaticResource DefaultStyle}">  
            <DataGrid.Columns>  
              <DataGridTextColumn  
                     Binding="{Binding Label}"  
                     Header="Site Model"  
                     IsReadOnly="True" />  
              <DataGridTextColumn  
                     Binding="{Binding Number}"  
                     Header="# Parts"  
                     IsReadOnly="True" />  
              <DataGridComboBoxColumn  
                     Width="*"  
                     DisplayMemberPath="Label"  
                     Header="Pro Model"  
                     ItemsSource="{Binding ProModels, Source={StaticResource vm}}"  
                     SelectedValuePath="ID"  
                     SelectedValueBinding="{Binding FK}"/>  
            </DataGrid.Columns>  
          </DataGrid>  
          <ComboBox  
             x:Name="Populated"  
             Grid.Row="1"  
             DisplayMemberPath="Label"  
             ItemsSource="{Binding ProModels}" />  
        </Grid>  
      </Grid>  
    </Window>  
    

    and classes see attached txt: 63073-x.txt


2 additional answers

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2021-02-02T12:11:22.367+00:00

    Hi David,
    in your code ItemsSource of DataGridComboBoxColumn is pointed to property of data item. You must use DataContext of DataGrid for ItemsSource. Try following XAML code:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <DataGrid
            AutoGenerateColumns="False"
            ItemsSource="{Binding SiteModels}"
            Style="{StaticResource DefaultStyle}">
            <DataGrid.Columns>
                <DataGridTextColumn
                    Binding="{Binding Label}"
                    Header="Site Model"
                    IsReadOnly="True" />
                <DataGridTextColumn
                    Binding="{Binding Number}"
                    Header="# Parts"
                    IsReadOnly="True" />
                <DataGridComboBoxColumn
                    x:Name="NotPopulated"
                    Width="*"
                    DisplayMemberPath="Label"
                    Header="Pro Model"
                    ItemsSource="{Binding DataContext.ProModels, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                    SelectedItemBinding="{Binding SelectedItem}" />
            </DataGrid.Columns>
        </DataGrid>
        <ComboBox
            x:Name="Populated"
            Grid.Row="1"
            DisplayMemberPath="Label"
            ItemsSource="{Binding ProModels}" />
    </Grid>
    
    0 comments No comments

  2. David McCallum 51 Reputation points
    2021-02-04T16:20:23.567+00:00

    Peter,

    FYI. I ended up solving it with a combination of your appreciated help and from binding-datagridcomboboxcolumn

    Once again, thank you very musch for your help.

    David

    <DataGridComboBoxColumn
                                Width="*"
                                DisplayMemberPath="Label"
                                Header="Pro Model">
                                <DataGridComboBoxColumn.ElementStyle>
                                    <Style>
                                        <Setter Property="ComboBox.ItemsSource" Value="{Binding DataContext.ProModels, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                    </Style>
                                </DataGridComboBoxColumn.ElementStyle>
                                <DataGridComboBoxColumn.EditingElementStyle>
                                    <Style>
                                        <Setter Property="ComboBox.ItemsSource" Value="{Binding DataContext.ProModels, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                    </Style>
                                </DataGridComboBoxColumn.EditingElementStyle>
                            </DataGridComboBoxColumn>
    
    0 comments No comments