将 ObservableCollection 绑定到 WPF 中的 DataGrid 的 TwoWay 绑定不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14607501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TwoWay Binding ObservableCollection to DataGrid in WPF not working
提问by Hossein Narimani Rad
I have this simple DataGrid in my application. Somewhere in the source I bind the ItemsSourceproperty of it to an ObservableCollection<System.Windows.Points>. So the points are shown in the DataGrid. The problem is however I have set the TwoWaybinding but when changing the point coordinate values in the DataGrid, actual point values int the ObservableCollectionare not changed!
我的应用程序中有这个简单的 DataGrid。在源代码的某个地方,我将ItemsSource它的属性绑定到ObservableCollection<System.Windows.Points>. 所以这些点显示在DataGrid. 但是问题是我已经设置了TwoWay绑定,但是在更改 中的点坐标值时DataGrid,实际点值 intObservableCollection没有更改!
What is going wrong?
出了什么问题?
<DataGrid Name="pointList" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="X" Width="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=X, Mode=TwoWay}"></TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Y" Width="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Y, Mode=TwoWay}"></TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
NoteI have seen thisbut my problem is different.
注意我已经看到了这个,但我的问题是不同的。
采纳答案by Cédric Bignon
System.Windows.Pointsis a struct. You can't bind its properties correctly.
System.Windows.Points是一个结构体。您无法正确绑定其属性。
Why? Because when you do Text="{Binding X, Mode=TwoWay}"it will bind the Textproperty of the TextBoxto the Xproperty of the current DataContext.
为什么?因为当你这样做时,Text="{Binding X, Mode=TwoWay}"它会将 的Text属性绑定TextBox到X当前的属性DataContext。
DataContextwhich is... a structSystem.Windows.Pointsthen the Pointthe databinding will modify is not the one you have assigned to DataContext.
DataContext这是...一个结构,System.Windows.Points那么Point数据绑定将修改的不是您分配给的那个DataContext。
To solve your problem. Create your own Pointtype using a class:
来解决你的问题。Point使用类创建您自己的类型:
public class Point : INotifyPropertyChanged
{
private double x;
public double X
{
get { return x; }
set
{
if (x != value)
{
x = value;
OnPropertyChanged("X");
}
}
}
private double y;
public double Y
{
get { return y; }
set
{
if (y != value)
{
y = value;
OnPropertyChanged("Y");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
and use UpdateSourceTrigger=LostFocusfor your binding:
并UpdateSourceTrigger=LostFocus用于您的绑定:
<TextBox Text="{Binding Path=Y, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"></TextBox>

