[英]WPF DataGrid Binding only works one-way
I want to bind my DataGrid columns. 我想绑定我的DataGrid列。 First I create the columns in DataGrid:
首先,我在DataGrid中创建列:
translationDataGrid = new DataGrid
{
IsReadOnly = true,
};
var fact = new FrameworkElementFactory(typeof(CheckBox));
fact.SetBinding(CheckBox.IsCheckedProperty, new Binding("Check") {Mode = BindingMode.TwoWay});
translationDataGrid.Columns.Add(new DataGridTemplateColumn
{
CellTemplate = new DataTemplate {VisualTree = fact}
});
translationDataGrid.Columns.Add(new DataGridTextColumn
{
Header = "Name",
Binding = new Binding("Name"),
Width = 250
});
Than I have a class which I use to create objects to add to DataGrid: 比起我用来创建要添加到DataGrid的对象的类来说,它更是如此:
private class ObjectToDataGrid : INotifyPropertyChanged
{
private bool _check;
public bool Check
{
get { return _check; }
set
{
_check = value;
NotifyPropertyChanged("Check");
}
}
public string Name { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
}
}
And here I add objects to DataGrid: 在这里,我将对象添加到DataGrid:
public void AddToDataGrid(string tag)
{
translationDataGrid.Items.Add(
new ObjectToDataGrid
{
Check = false,
Name = tag,
});
}
The problem is, that it only changes one-way. 问题在于,它只能单向更改。 If I change the data, like this:
如果我更改数据,如下所示:
foreach (ObjectToDataGrid row in translationDataGrid.Items)
{
row.Check = check;
}
data in grid change as they supposed to. 网格中的数据将按预期方式更改。 But when I check the checkBox, and than try to retrieve Checked value from the underlying object, its unchanged.
但是,当我检查checkBox并尝试从基础对象检索Checked值时,它保持不变。
I've been looking for a solution for several hours now, but I can't find it. 我一直在寻找解决方案已有几个小时,但找不到。 Can someone please help?
有人可以帮忙吗?
try set UpdateSourceTrigger
to UpdateSourceTrigger.PropertyChanged
尝试将
UpdateSourceTrigger
设置为UpdateSourceTrigger.PropertyChanged
new Binding("Check") {
Mode = BindingMode.TwoWay ,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.