question

PierreMRQ-4021 avatar image
0 Votes"
PierreMRQ-4021 asked PierreMRQ-4021 edited

UWP - datagrid not updating when observable collection is changed

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.

windows-uwp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

We can't reproduce the problem with your code, could mind share a code sample or told us how to remove the item from ObservableCollection ?

0 Votes 0 ·

Hello, thank you for your responsiveness as always.
The problem was simply that I used an ICollection instead of an ObservableCollection for the second Datagrid. It's while doing the sample that I realized that. It's working perfectly now i just replaced the ICollection by the Observable.

0 Votes 0 ·

1 Answer

PierreMRQ-4021 avatar image
0 Votes"
PierreMRQ-4021 answered PierreMRQ-4021 edited
 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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.