Search code examples
xamluwpdatagridwindows-community-toolkit

ComboBox Binding Problem with Windows Community Toolkit Datagrid


Let's say I've got a DataGrid bound to a collection of type AreaVM. AreaVM has a property called InitialSub of type Sub. The ItemsSource of the combobox is another collection of type Sub.

<wct:DataGrid x:Name="grid"
              ItemsSource="{x:Bind ViewModel.Source, Mode=TwoWay}"
              Margin="12"
              FontSize="4"
              AutoGenerateColumns="False"
              GridLinesVisibility="None"
              CanUserResizeColumns="True"
              CanUserSortColumns="True"
              SelectionMode="Extended"
              IsReadOnly="False"
              VerticalScrollBarVisibility="Auto"    
              HorizontalScrollBarVisibility="Auto">
    <wct:DagaGridColumns>
        <wct:DataGridComboBoxColumn Binding="{Binding InitialSub, Mode=TwoWay}" Tag="InitialSub"
                            Header="Initial Sub"
                            Width="220"
                            ItemsSource="{x:Bind ViewModel.SourceForSubsList}"
                            DisplayMemberPath="SubName"
        </wct:DataGridComboBoxColumn>
    </wct:DataGridColumns>
</wct:DataGrid>

Why do I get this error: "The ItemsSource elements do not contain a property InitialSub. Ensure that the binding path has been set correctly." Can anyone help?

The pertinent parts of the page's viewmodel are as follows:

public ObservableCollection<Sub> SourceForSubsList { get; set; }

public ObservableCollection<AreaVM> Source
    {
        get => _source;
        set
        {
            _source = value;
        }
    }

The AreaVM contains all the properties for each row of the datagrid. One of these properties is InitialSub:

 public Subfactor InitialSub
    {
        get => Model.InitialSub;
        set
        {
            if (value != Model.InitialSub && value != null)
            {
                Model.InitialSub = value;
                RaisePropertyChanged(nameof(InitialSub));                     
            }
        }
    }

The DataGrid does load correctly if I don't include the ComboBox columns.

I changed the ComboBox XAML to this but I'm still getting the same error:

<wct:DataGridComboBoxColumn Binding="{Binding InitialSub, Mode=TwoWay}" 
                            Header="Initial Sub"
                            Width="220"
                            DisplayMemberPath="SubName"    
                            ItemsSource="{x:Bind ViewModel.SourceForSubsList}"
                            Visibility="{x:Bind ViewModel.ShowInitialCoreColumns, Mode=OneWay, Converter={StaticResource boolToVisConverter}}">
</wct:DataGridComboBoxColumn>

The SubName property is a string. It is like the DisplayMemberPath is being ignored though. I still get this: "The ItemsSource elements do not contain a property InitialSub. Ensure that the binding path has been set correctly."


Solution

  • I finally got this working. I tried changing the binding of the ItemsSource for the whole grid from "x:Bind" to "Binding." That fixed the issue, but I'm not sure why.

    I used the following for the ComboBox:

    <wct:DataGridComboBoxColumn 
        Binding="{Binding InitialSub, Mode=TwoWay}" 
        Tag="InitialSub"                                            
        Header="Initial Sub"                                              
        Width="220"                                           
        IsReadOnly="False"                                                
        ItemsSource="{x:Bind ViewModel.SourceForSubsList}">