Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGrid SelectedItem not updating

Tags:

c#

wpf

datagrid

So I am reallly confused here.

I created a datagrid, bound its itemsource two way and bound its selected item two way. The selected item getter gets called but the setter never does. All the pieces 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 don't bind to the selected item. The items sources work but that is 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 displayed in the output window.

like image 539
Anthony Russell Avatar asked Feb 03 '15 12:02

Anthony Russell


2 Answers

The answer to this is pretty obvious actually.

I forgot to put UpdateSourceTrigger=PropertyChanged

so it should look 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

like image 141
Anthony Russell Avatar answered Sep 27 '22 21:09

Anthony Russell


I faced something similar to that a couple of months ago, for some reason the inner DataGrid ItemSource wasn't set correctly using that way, i managed to fix that by binding using ElementName to set the RowDetail DataGrid ItemSource to the SelectedItem in the parent one :

<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}" 
like image 41
SamTh3D3v Avatar answered Sep 27 '22 22:09

SamTh3D3v