Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update DataGrid cell if other cell changes

I have a View that displays a Part. All parts contain a list of identifiers. In my View I display Part Properties and a DataGrid with all the Identifiers of that part.

Now if I change a value of an identifier, I want another value update to the default. But if I change my identifier value and set the default of the other property - my DataGrid does not update. Only if I click on the cell, then it gets updated after losing focus.

How can I update the View automatically?

I guess the problem is that I do not want to update a direct property of the Part, but a Property in a List that is a property of the Part.

View

<DataGrid>
    <DataGridTemplateColumn Header="Company">
        <DataGridTemplateColumn.CellEditingTemplate>
             <DataTemplate>
                  <ComboBox x:Name="CompanyEditComboBox" 
                            ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Companies}" 
                            SelectedItem="{Binding Company, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                           SelectionChanged = "CompanyEditComboBox_SelectionChanged" />
            </DataTemplate>
         </DataGridTemplateColumn.CellEditingTemplate>
         <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                 <TextBlock Text="{Binding Company}" />
            </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
     </DataGridTemplateColumn>


    <DataGridTemplateColumn Header="CompanyType">
         <DataGridTemplateColumn.CellEditingTemplate>
              <DataTemplate>
                    <ComboBox x:Name="CompanyTypeEditComboBox" 
                                      ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.CompanyTypes}" 
                                      SelectedItem="{Binding IdentificationCompanyType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
              </DataTemplate>
          </DataGridTemplateColumn.CellEditingTemplate>
          <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                 <TextBlock Text="{Binding IdentificationCompanyType, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
              </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>
       </DataGridTemplateColumn>
</DataGrid>

View Code-Behind

private void CompanyEditComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var vm = (PartViewModel)DataContext;
    var box = (ComboBox) sender;
    var c = (Company) box.SelectedItem;
    vm.SetDefaultCompanyType(c);
}

ViewModel

public void SetDefaultCompanyType(Company c)
{
    SelectedIdentification.IdentificationCompanyType = c.DefaultCompanyType;
    OnPropertyChanged("IdentificationCompanyType");
}
like image 386
juergen d Avatar asked Jun 09 '15 19:06

juergen d


Video Answer


1 Answers

Solved it. I had to add a

OnPropertyChanged("IdentificationCompanyType");

into the setter of the IdentificationCompanyType in the Identification class. After that it got automatically updated in the DataGrid.

like image 68
juergen d Avatar answered Sep 29 '22 08:09

juergen d