我在DataGrid中有DataGridComboBoxColumn:
<DataGridComboBoxColumn Header="{x:Static Properties:Resources.Unit}" Width="Auto"
SelectedValueBinding="{Binding UnitUnitId}"
SelectedValuePath="UnitId"
DisplayMemberPath="Name" >
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.Units,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.Units,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
和ComboBox:
<ComboBox x:Name="unitBox" ItemsSource="{Binding Path=Units}"
SelectedItem="{Binding TaxSubGroup.Unit}"
Grid.Row="2"
Grid.Column="1" Margin="11,11,0,0" HorizontalAlignment="Stretch">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={x:Static c:UnitToStringConverter.Default}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
单位属性:
public ObservableCollection<Unit> Units
{
get
{
return _units;
}
set
{
if(_units!=value)
{
_units = value;
RaisePropertyChanged("Units");
}
}
}
当我更改ComboBoxes值时,DataGridComboBoxColumn的值会自动更新,但更改DataGrid的comboboxcolumn值不会更新ComboBoxes值。为什么呢?
已更新
问题出在DataGridComboBoxCulumn的SelectedValueBinding属性中。我只需要添加UpdateSourceTrigger属性:
SelectedValueBinding="{Binding UnitUnitId,UpdateSourceTrigger=PropertyChanged}"
答案 0 :(得分:0)
您是否在TaxSubGroup.Unit
属性的设置者中更新了UnitUnitId
?
在您的DataContext模型类中执行与此类似的操作...
public int UnitUnitId
{
get
{
return this.unitID;
}
set
{
this.unitID = value;
this.TaxSubGroup.Unit = Units.FirstOrDefault(u => u.UnitID == value);
this.NotifyPropertyChange("UnitUnitId");
}
}
这将通过引用匹配SelectedItem
的{{1}}。