【发布时间】:2015-12-02 21:36:35
【问题描述】:
我有一个 DataGrid,它的 ItemsSource 设置为自定义对象的 ObservableCollection,它从窗口传递到包含 DataGrid 的用户控件,虽然这工作得很好,并且在两个方向上都注册了更改我想如果我单击取消按钮,则可以取消在我的 UserControl 中所做的任何更改。
有什么方法可以将我在 DataGrid 中所做的任何更改推迟到父窗口中的 ItemSource 吗?或者相反,如果我愿意,可以通过某种方式取消更改。任何帮助将不胜感激,这是我当前的代码;
public UserControl1(ObservableCollection<ContourControl.RectangleContour> list1)
{
InitializeComponent();
itemCollectionViewSource = (CollectionViewSource)(FindResource("ItemCollectionViewSource"));
itemCollectionViewSource.Source = list1;
//here i create some columns and bind the data
DataGridViewer.ItemsSource = list1;
DataGridViewer.AutoGenerateColumns = false;
}
private void Cancel(object sender, RoutedEventArgs e)
{
//if i click this i would like any changes to list1 to be reverted or ignored
Window parentWindow = Window.GetWindow((DependencyObject)sender);
if (parentWindow != null)
{
parentWindow.Close();
}
}
private void OK(object sender, RoutedEventArgs e)
{
//if i click this i would like the changes to carry on to my list, as it currently does instantly
Window parentWindow = Window.GetWindow((DependencyObject)sender);
if (parentWindow != null)
{
parentWindow.Close();
}
}
这是启动我的用户控件并传递我存储数据的原始列表的代码。
Window window = new Window
{
Height = 200,
Width = 765,
Content = new UserControl1(list1: rectContourList),
};
window.ShowDialog();
【问题讨论】:
-
您基本上是在问如何实现撤消功能。这个问题已经解决了这个问题:stackoverflow.com/questions/7984674/…
标签: c# .net wpf wpfdatagrid