0
votes

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."

1
Hi Matt, I have not saw you bound InitialCoreFindingSubfactor to ItemsSource, I could not reproduce your issue, could you mind share minimal reproducible example?Nico Zhu - MSFT
Thanks, Nico. Sorry, the error message should have read ""The ItemsSource elements do not contain a property InitialSub." I also added the XAML that shows the DataGrid setup.Matt
Does ViewModel contain SourceForSubsList field ? Could you show the your viewmodel?Nico Zhu - MSFT
It does. I have added the viewmodel above. Thanks.Matt
I found your problem, you need to convert SourceForSubsList to string List or make DisplayMemberPath direct to string property of Sub class.Nico Zhu - MSFT

1 Answers

0
votes

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}">