我正在使用WPF Toolkit DataGrid和DataGridComboBoxColumn。一切都运行良好,除了在组合框中发生选择更改时,所选的值绑定源不会立即更新。仅当组合框失去焦点时才会发生这种情况。有没有人遇到过这个问题和任何建议解决方案?
这是列的xaml:
<toolkit:DataGridComboBoxColumn Header="Column" SelectedValueBinding="{Binding Path=Params.ColumnName, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="cName"
SelectedValuePath="cName">
<toolkit:DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Info.Columns}" />
</Style>
</toolkit:DataGridComboBoxColumn.ElementStyle>
<toolkit:DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Info.Columns}" />
</Style>
</toolkit:DataGridComboBoxColumn.EditingElementStyle>
</toolkit:DataGridComboBoxColumn>
答案 0 :(得分:11)
UpdateSourceTrigger=PropertyChanged
选项在这里至关重要,它离不开它。
答案 1 :(得分:3)
问题是,在您离开单元格并提交更改
之前,单元格仍处于编辑模式解决方案:您需要创建自己的列类型以覆盖默认行为
代码:
public class AutoCommitComboBoxColumn : Microsoft.Windows.Controls.DataGridComboBoxColumn
{
protected override FrameworkElement GenerateEditingElement(Microsoft.Windows.Controls.DataGridCell cell, object dataItem)
{
var comboBox = (ComboBox)base.GenerateEditingElement(cell, dataItem);
comboBox.SelectionChanged += ComboBox_SelectionChanged;
return comboBox;
}
public void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
CommitCellEdit((FrameworkElement)sender);
}
}