DataGrid SelectedItem not updating

So I'm embarrassed again.

I created a datagrid, linked its source of goods in two ways, and linked my selected item in two ways. The selected getter is invoked, but the setter never does. All the figures seem to be here. What am I missing?

<DataGrid ItemsSource="{Binding Properties ,Mode=TwoWay}" SelectedItem="{Binding SelectedProperty ,Mode=TwoWay}" CanUserDeleteRows="False" CanUserAddRows="False" AutoGenerateColumns="False" Background="LightBlue"> <DataGrid.Columns> <DataGridTextColumn IsReadOnly="True" Header="Address" Binding="{Binding Address}"/> </DataGrid.Columns> <DataGrid.RowDetailsTemplate> <DataTemplate> <DataGrid ItemsSource="{Binding Units ,Mode=TwoWay}" SelectedItem="{Binding SelectedUnit, Mode=TwoWay}" CanUserDeleteRows="False" CanUserAddRows="False" AutoGenerateColumns="False"> 

My first datagrid works fine, including the selected item.

The second third and fourth nested grids, however, are not tied to the selected item. Element sources work but it

  public class PropertyModel : ModelBase { private ObservableCollection<UnitModel> _Units; public ObservableCollection<UnitModel> Units { get { return _Units; } set { _Units = value; } } private UnitModel _SelectedUnit; public UnitModel SelectedUnit { get { return _SelectedUnit; } set { _SelectedUnit = value; OnPropertyChanged("SelectedUnit"); } } 

There are no binding expression errors or any other errors in the output window.

+5
source share
2 answers

The answer to this is pretty obvious.

I forgot to put UpdateSourceTrigger = PropertyChanged

so that it looks like this:

  <DataGrid ItemsSource="{Binding Units ,Mode=TwoWay}" SelectedItem="{Binding SelectedUnit,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" CanUserDeleteRows="False" CanUserAddRows="False" AutoGenerateColumns="False"> 

This solved the problem.

+5
source

I came across something similar to the fact that a couple of months ago, for some reason, the internal DataGrid ItemSource was incorrectly configured using this method, I was able to fix this by contacting ElementName to set the RowDetail DataGrid ItemSource to SelectedItem in the parent :

 <DataGrid x:Name="DataGrid" ItemsSource="{Binding Properties ,Mode=TwoWay}" SelectedItem="{Binding SelectedProperty ,Mode=TwoWay}" CanUserDeleteRows="False" CanUserAddRows="False" AutoGenerateColumns="False" Background="LightBlue"> <DataGrid.Columns> <DataGridTextColumn IsReadOnly="True" Header="Address" Binding="{Binding Address}"/> </DataGrid.Columns> <DataGrid.RowDetailsTemplate> <DataTemplate> <DataGrid ItemsSource="{Binding SelectedItem.Units ,Mode=TwoWay,ElementName=DataGrid}" 
+1
source

Source: https://habr.com/ru/post/1212531/


All Articles