我有一个像这样的WPF DataGrid:
<DataGrid
ItemsSource="{Binding Items}"
SelectionUnit="CellOrRowHeader">
</DataGrid>
它的约束如下:
public partial class MainWindow : Window
{
public ObservableCollection<Item> Items { get; set; }
public MainWindow()
{
InitializeComponent();
Items = new ObservableCollection<Item>
{
new Item()
};
this.DataContext = this;
}
}
public class Item
{
private string _foo = string.Empty;
public string Foo
{
get { return _foo; }
set { _foo = value; }
}
private string _bar = string.Empty;
public string Bar
{
get { return _bar; }
set { _bar = value; }
}
}
该程序提供了一个项目。如果我在网格上更改Foo和Bar,即使我在同一行的单元格之间切换,它也不会绑定更改。但是,如果我点击下一行,所有更改立即被绑定。我猜这与默认的UpdateSourceTrigger设置为LostFocus有关。但是我希望它在Cell失去焦点而不是Row时绑定,因为我已经为单元格选择配置了网格。我怎么能这样做?
答案 0 :(得分:1)
我不知道为自动生成列更改UpdateSourceTrigger
的其他方法,然后更改AutoGeneratingColumn
事件
<DataGrid ItemsSource="{Binding Items}" SelectionUnit="CellOrRowHeader" AutoGeneratingColumn="myDataGrid_AutoGeneratingColumn"/>
然后在代码中
private void myDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
var column = e.Column as DataGridBoundColumn;
if (column != null)
{
var binding = column.Binding as Binding;
if (binding != null) binding.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
}
}