我有一个视图显示零件.所有部分都包含标识符列表.在我的视图中,我显示零件属性和DataGrid,其中包含该零件的所有标识符.
现在,如果我更改一个标识符的值,我想要另一个值更新为默认值.但是,如果我更改了我的标识符值并设置了其他属性的默认值 – 我的DataGrid不会更新.只有当我点击单元格,那么在失去焦点后才能更新.
如何自动更新视图?
我想问题是,我不想更新该零件的直接属性,而是更新该零件属性的列表中的属性.
视图
<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,UpdateSourceTrigger=PropertyChanged}" /> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding IdentificationCompanyType,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid>
查看代码后
private void CompanyEditComboBox_SelectionChanged(object sender,SelectionChangedEventArgs e) { var vm = (Partviewmodel)DataContext; var Box = (ComboBox) sender; var c = (Company) Box.SelectedItem; vm.SetDefaultCompanyType(c); }
视图模型
public void SetDefaultCompanyType(Company c) { SelectedIdentification.IdentificationCompanyType = c.DefaultCompanyType; OnPropertyChanged("IdentificationCompanyType"); }