我想在UpdateSourceTrigger事件中将DataGridTextColumn绑定的LostFocus设置为"LostFocus“。默认的行为是,只有当行失去焦点时才更新源,但当单元格失去焦点时,我需要它进行更新。
以下是我的尝试:
private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
//my attempt to get cell to update on cell lost-focus, rather than row lost-focus
Binding newBinding = new Binding(e.PropertyName);
newBinding.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
(e.Column as DataGridTextColumn).Binding = newBinding;
}这似乎不起作用,因为只有当行失去焦点时,才会更新源。
注意:我知道这通常是在列定义中的XAML中完成的,但是我不能把它放在那里,因为我的列是在运行时动态生成的。
发布于 2022-07-27 16:39:31
在我的例子中,我需要在单击DataGrid之后立即更改一个布尔属性,下面的代码工作正常:
private void ExcludeTable_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.Column is DataGridCheckBoxColumn column && column.Binding is System.Windows.Data.Binding binding)
{
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
}
}https://stackoverflow.com/questions/47019038
复制相似问题