UWP - datagrid not updating when observable collection is changed

Pierre MRQ 136 Reputation points
2021-05-30T20:15:39.07+00:00

Hello,
I have a datagrid on two levels, I manage to update my UI for the first datagrid, but i have a problem for the second one.
Although I can update the properties in the grid, when i want to remove/add a new row (with an observablecollection), the UI is not updating while the collection is modified.

I think the issue comes from the datacontext of the the second grid, but I have no more ideas that I haven't tried to make this thing work...

Here is my simplified code to illustrate the problem:

XAML:

    <controls:DataGrid
                x:Name="Maingrid"
                IsReadOnly="True"
                ItemsSource="{x:Bind ViewModel.Source, Mode=OneWay}">
            <controls:DataGrid.Columns>
                <controls:DataGridTextColumn Header="p1" Binding="{Binding p1}" IsReadOnly="True"/>
                <controls:DataGridTextColumn Header="p2" Binding="{Binding p2}" IsReadOnly="True"/>
            </controls:DataGrid.Columns>
            <controls:DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Grid>
                            <controls:DataGrid
                            x:Name="RowDatagrid"
                            ItemsSource="{Binding Path=Source2, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
                                <controls:DataGrid.Columns>
                                    <controls:DataGridTextColumn Header="p3" Binding="{Binding p3}" IsReadOnly="True"/>
                                    <controls:DataGridTextColumn Header="p4" Binding="{Binding p4}" IsReadOnly="True"/>
                                </controls:DataGrid.Columns>
                            </controls:DataGrid>
                        </Grid>
                    </StackPanel>
                </DataTemplate>
            </controls:DataGrid.RowDetailsTemplate>
        </controls:DataGrid>

ViewModel:

       public class TestObjectViewModel : INotifyPropertyChanged
        {
            public ObservableCollection<TestObject> Source { get; set; } = new ObservableCollection<TestObject>();
            ...
        }

First datagrid object:

public class TestObject : INotifyPropertyChanged
        {
            public string p1 { get; set; }
            public int p2 { get; set; }
            public ICollection<TestObjectDetail> Source2 { get; set; }

            public event PropertyChangedEventHandler PropertyChanged;

            public void OnPropertyChanged([CallerMemberName] string isAsigned = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(isAsigned));
            }
        }

Second datagrid object:

public class TestObjectDetail : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public int p3 { get; set; }
        public int p4 { get; set; }
        ...
    }

Thank you in advance for your help.

Developer technologies | Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Pierre MRQ 136 Reputation points
    2021-05-31T08:25:13.257+00:00
    public class TestObject : INotifyPropertyChanged
             {
                 public string p1 { get; set; }
                 public int p2 { get; set; }
                 public ObservableCollection<TestObjectDetail> Source2 { get; set; }
    
                 public event PropertyChangedEventHandler PropertyChanged;
    
                 public void OnPropertyChanged([CallerMemberName] string isAsigned = null)
                 {
                     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(isAsigned));
                 }
             }
    

    Just used the right collection

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.