隐藏并在编辑模式下重新显示时的WPF DataGrid问题 [英] WPF DataGrid Problem when hidden and Reshown in Editing Mode

查看:50
本文介绍了隐藏并在编辑模式下重新显示时的WPF DataGrid问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

也许有人遇到以下情况:

我在tabcontrol 内容模板中放置了datagrid .

执行以下步骤时,会导致异常:

*我进入其中带有datagrid的选项卡
*在数据网格中编辑文本项,但将插入号保留在字段中
*切换到另一个标签
*切换回带有已编辑的datagrid的标签页

异常如下:

Hi everyone,

maybe someone has come around the following situation:

I have placed a datagrid inside a tabcontrol content template.

When performing the following steps, an exception is caused:

* I go into the tab with the datagrid inside
* edit an text item in the datagrid but leave the caret in the field
* Switch to another tab
* Switch back to the tab with the edited datagrid

The exception is as follows:

InvalidOperationException {"'DeferRefresh' is not allowed during an AddNew or EditItem transaction."}



因此,我不得不手动结束编辑模式(可能是DataGrid.CancelEdit()).但是,如何在MVVM中做到这一点?

非常感谢!



From that I take that I would have to end the editing mode (probably DataGrid.CancelEdit()) manually. But how do I do this in MVVM?

Many thanks!

推荐答案

这不是解决方案,而只是一种解决方法.我已经为数据网格创建了一个附加属性,该属性将链接到视图模型中的通知bool属性.当翻转该布尔值时,数据网格的编辑模式结束.在我的代码中,当包含数据网格的tab控件的SelectedItem发生更改时,我会翻转该布尔值.

This is not a solution, merely a workaround. I''ve created an attached property for the datagrid that will be linked to a notifying bool property in the viewmodel. When flipping that bool, the editing mode of the datagrid is ended. In my code, I''m flipping that bool when the SelectedItem of the tabcontrol which contains the datagrid changes.

public class DataGridBehaviors
{
  // The purpose of this property is to be able to signal a datagrid manually 
  // to end its editing mode. When you have datagrid on a tab inside a tabcontrol
  // based on content templates and a observablecollection as source, it may cause an
  // exception when leaving and reentering it in editing mode (while the caret is in 
  // a cell)
#region EndEditing Property
  // When starting this property, I first tried to generate an action right here and 
  // call it from the viewmodel. This didn''t work out so I went for having a simple
  // bool which - when flipped - signals the datagrid to stop the editing mode.
  
  public static void SetEndEditing(DependencyObject obj, bool a)
  {
    obj.SetValue(EndEditingProperty, a);
  }

  public static bool GetEndEditing(DependencyObject obj)
  {
    return (bool) obj.GetValue(EndEditingProperty);
  }

  public static readonly DependencyProperty EndEditingProperty
    = DependencyProperty.RegisterAttached(
    "EndEditing",
    typeof(bool),
    typeof(DataGridBehaviors),
    new PropertyMetadata(new PropertyChangedCallback(EndEditingChanged))
  );

  private static void EndEditingChanged(DependencyObject sender, 
    DependencyPropertyChangedEventArgs e)
  {
    var dg = sender as DataGrid;
    if (null == dg) return;

    // CancelEdit is not sufficient
    dg.CancelEdit();
    // this has also to be called to really end the editing mode
    dg.CommitEdit();
  }
#endregion
}


您遇到的问题,不是您认为的问题.基本上,当您在WPF中切换选项卡时,旧的数据上下文和视图将被破坏,而新的上下文和视图将被创建.如果您下载 Cinch [ ^ ],并查看WPF_Demo.zip项目,您将找到一个TabControlEx类;用它代替您的TabControl,这个问题就应该解决了.
The problem you''ve encountered, isn''t the problem that you think it is. Basically, when you switch tabs in WPF, the old data context and view are destroyed, and a new one is created. If you download Cinch[^] and take a look at the WPF_Demo.zip project, you will find a TabControlEx class; use this in place of your TabControl and this problem should go away.


这篇关于隐藏并在编辑模式下重新显示时的WPF DataGrid问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆