【发布时间】:2015-11-09 23:50:07
【问题描述】:
我在将列表数据绑定到数据网格时遇到了一些问题。我的代码如下。
我想绑定我的 observable 集合;
ObservableCollection<RectangleContour> rectContourList = new ObservableCollection<RectangleContour>();
矩形轮廓对象;
public class RectangleContour : INotifyPropertyChanged
{
public int Property1{ get; set; }
public int Property2{ get; set; }
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged( String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我想将列表绑定到另一个窗口中存在的数据网格视图,该窗口从上述对象所属的窗口调用,我这样调用新窗口;
Window window = new Window
{
Title = "My User Control Dialog",
Content = new UserControl1(list1: rectContourList),
};
window.ShowDialog();
window.Close();
数据正确显示,如果我编辑网格上已经存在的元组,则更改会像我想要的那样反映在我的 MainWindow 中,但是我也希望能够通过数据网格添加新对象,并且当我尝试这样做时,我在 window.close() 上得到一个异常,就像说我需要 Path 或 xPath 来实现 TwoWay Bind。
谁能指出我正确的方向,为什么会发生这种情况?我是否需要在两个窗口之间以及数据网格和可观察集合之间设置数据绑定?
感谢您的帮助。
【问题讨论】:
标签: c# wpf winforms data-binding datagrid