问题描述
我正在使用 WPF 工具包 DataGrid 和 DataGridComboBoxColumn.一切正常,除了当组合框上发生选择更改时, selectedvaluebinding 源不会立即更新.只有当组合框失去焦点时才会发生这种情况.有没有人遇到过这个问题和任何建议解决方案?
I'm using WPF Toolkit DataGrid and DataGridComboBoxColumn. Everything works well, except that when selection change happens on the combobox, the selectedvaluebinding source is not updated immediately. This happens only when the combobox loses focus. Has anyone run into this issue and any suggestions solutions ?
这是该列的 xaml:
Here's the xaml for the column:
<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>
推荐答案
问题是单元格一直处于编辑模式,直到您离开单元格并提交更改
The problem is that the cell remains in Edit mode until you leave the cell and the changes are committed
解决方案:您需要创建自己的列类型来覆盖默认行为
Solution: you need to create your own column type to override the default behavior
代码:
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);
}
}
这篇关于WPFToolkit DataGrid:Combobox 列不会立即更新 selectedvaluebinding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!