【问题标题】:WPF DataGrid - Retain selection when disablingWPF DataGrid - 禁用时保留选择
【发布时间】:2011-07-07 12:57:27
【问题描述】:

我已经为此苦苦挣扎了一段时间。我的应用程序中有一个 Master / Details 布局,并且像许多其他人一样面临着 DataGrid 在禁用它时失去选择的问题。本质上,在从列表中选择一个元素以填充一系列字段后,用户按下“编辑”,这将禁用 DataGrid 并启用所有表单的字段。保存数据后,按下“保存”按钮将恢复这些操作......非常困难。

我在 Windows 7 上使用 .Net Framework 4 中的 VS 2010 进行开发。

我尝试过的:
1) 基于this post,我曾尝试在2009年6月版的WPF工具包中使用DataGrid,但我有同样的反应。
2)基于this WPF CodePlex bug report,我尝试创建一个基于DataGrid的自定义控件并覆盖OnIsEnabledChanged调用以删除对“UnselectAllCells”的调用,但没有代码示例,我什至无法触发一次。我试过了:

public class FormMainDataGrid : DataGrid
{
    static FormMainDataGrid()
    {
        IsEnabledProperty.OverrideMetadata(typeof(FormMainDataGrid), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnIsEnabledChanged)));
    }

    public FormMainDataGrid() : base() { }

    private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        d.CoerceValue(CanUserAddRowsProperty);
        d.CoerceValue(CanUserDeleteRowsProperty);

        //this was added in new version !!!
        /*
        if (!(bool)(e.NewValue))
        {
            ((DataGrid)d).UnselectAllCells();
        }
        */

        // Many commands use IsEnabled to determine if they are enabled or not
        CommandManager.InvalidateRequerySuggested();
    }
}  

但是一旦我禁用 DataGrid,这仍然会取消选择当前选定的行。我试图像这样解释最后的 cmets(在 Codeplex 错误报告中):

public class FormMainDataGrid : DataGrid
{
    static FormMainDataGrid()
    {

    }

    public static void OverrideStuff() 
    {
        IsEnabledProperty.OverrideMetadata(typeof(FormMainDataGrid), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnIsEnabledChanged)));
    }

    public FormMainDataGrid() : base() { }

    private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        d.CoerceValue(CanUserAddRowsProperty);
        d.CoerceValue(CanUserDeleteRowsProperty);

        //this was added in new version !!!
        /*
        if (!(bool)(e.NewValue))
        {
            ((DataGrid)d).UnselectAllCells();
        }
        */

        // Many commands use IsEnabled to determine if they are enabled or not
        CommandManager.InvalidateRequerySuggested();
    }
}

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        FormMainDataGrid.OverrideStuff();
        base.OnStartup(e);
    }
}  

但这甚至不会触发该方法的修改版本。

首先,我是否走对了这条路?考虑到 Deselection 是由这种方法引起的,我可以完全替换我自己的方法对 'OnIsEnabledChanged' 的内部调用吗? 还有另一种方法可以解决这个问题吗? 或者更具体地说,我如何停止对该方法的基本版本的调用,因为它不是覆盖,因此我不能“不”调用base.OnIsEnabledChanged

非常感谢!

【问题讨论】:

    标签: c# wpf datagrid selection


    【解决方案1】:

    供将来参考,如果有人遇到同样的问题。
    重新设置 SelectedValue 有很多副作用。
    这是覆盖 Grid 上的元数据的正确方法:

    public class MyDataGrid : DataGrid
    {
        static MyDataGrid()
        {
            IsEnabledProperty.OverrideMetadata(typeof(MyDataGrid), new CustomFrameworkPropertyMetadata(OnIsEnabledChanged));
        }
    
        /// <summary>
        /// Fixes the issue that the DataGrid's selection is cleared whenever the DataGrid is disabled.
        /// Tricky: this issue only happens for 4.0 installations, it is fixed in 4.5 (in-place upgrade) installations.
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            d.CoerceValue(CanUserAddRowsProperty);
            d.CoerceValue(CanUserDeleteRowsProperty);
    
            //this is there in 4.0 dlls, not in the in-place upgrade 4.5 dlls.
            //if (!(bool)(e.NewValue))
            //{
            //    ((DataGrid)d).UnselectAllCells();
            //}
    
            CommandManager.InvalidateRequerySuggested();
        }
    
        class CustomFrameworkPropertyMetadata : FrameworkPropertyMetadata
        {
            public CustomFrameworkPropertyMetadata(PropertyChangedCallback propertyChangedCallback)
                : base(propertyChangedCallback)
            {
            }
    
            protected override void Merge(PropertyMetadata baseMetadata, DependencyProperty dp)
            {
                // See: http://msdn.microsoft.com/en-us/library/system.windows.propertymetadata.merge.aspx
                // See: http://msdn.microsoft.com/en-us/library/ms751554.aspx
                // By default, PropertyChangedCallbacks are merged from all owners in the inheritance hierarchy,
                // so all callbacks are called whenever the property changes.
                var thisPropertyChangedCallback = this.PropertyChangedCallback;
    
                base.Merge(baseMetadata, dp);
    
                // We do NOT want that default behavior here;
                // The callback of DataGrid should not be called here - it clears the selection, we don't want that.
                // But the callback of UIElement should be called here - it visually disabled the element, we still want that.
                if (baseMetadata.PropertyChangedCallback != null)
                {
                    Delegate[] invocationList = baseMetadata.PropertyChangedCallback.GetInvocationList();
                    PropertyChangedCallback inheritedPropertyChangedCallback = null;
                    foreach (var invocation in invocationList)
                    {
                        if (invocation.Method.DeclaringType == typeof(DataGrid))
                        {
                            // Do nothing; don't want the callback from DataGrid that clears the selection.
                        }
                        else
                        {
                            inheritedPropertyChangedCallback = inheritedPropertyChangedCallback == null
                                ? (PropertyChangedCallback)invocation
                                : (PropertyChangedCallback)Delegate.Combine(inheritedPropertyChangedCallback, invocation);
                        }
    
                    }
                    this.PropertyChangedCallback = thisPropertyChangedCallback != null
                                                       ? (PropertyChangedCallback)Delegate.Combine(inheritedPropertyChangedCallback, thisPropertyChangedCallback)
                                                       : inheritedPropertyChangedCallback;
                }
            }
        }
    }
    



    请注意,本文中提到的问题仅发生在未安装 4.5 的 4.0 安装中。
    它在 .net 4.5 中是“固定的”,即使是针对 4.0 的应用程序 (“4.5 是in-place upgrade”场景/misery)。

    问候,
    科恩

    【讨论】:

      【解决方案2】:

      出于这个原因,我通常不会专门禁用控件。我发现折叠保持其数据绑定当前的控件要好得多,或者如果我必须保持它可见但不允许任何类型的交互,则在其上放置一个部分透明的黑色边框,该边框通常折叠并在命令中可见。

      【讨论】:

      • 感谢勇士。覆盖的问题是,即使将 DataGrid Focusable = False、AllowKeyboardFocus = False,用户仍然可以使用选择(按键盘上的向上和向下箭头,出于某种原因将焦点导航到下一个控制直到它击中 DataGrid 并且实际上开始滚动它)。我意识到行细节模式更受鼓励,但我不是 UI 设计师,像这样的设计限制并不能真正让设计师高兴! ;-)
      • 尝试设置 IsHitTestVisible = False 就可以了。那时用户应该无法影响它。
      【解决方案3】:

      IsHitTestVisible = false 时,Up-Down 键仍然存在同样的问题。

      所以我最终做的是像这样重新设计自定义控件:

          public class FormMainDataGrid : DataGrid
          {
              public FormMainDataGrid() : base() {
                  this.IsEnabledChanged += new DependencyPropertyChangedEventHandler(DataGrid_IsEnabledChanged);
                  this.SelectionChanged += new SelectionChangedEventHandler(DataGrid_SelectionChanged);
              }
      
              private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs args)
              {
                  if (this.IsEnabled)
                  {
                      _selectedValue = this.SelectedValue;
                  }
              }
      
              private object _selectedValue;
      
              private void DataGrid_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs args)
              {
                  this.Dispatcher.BeginInvoke((Action)(() =>
                  {
                      this.SelectedValue = _selectedValue;
                  }), null);
              }
          }
      

      这很好用...我只需要小心,因为当控件禁用时更改 SelectedValue 会使其偏离轨道...

      因此,总而言之,我相信您的解决方案是最完整的,但我的解决方案让我可以让表单的代码尽可能简洁。

      感谢您的帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-24
        • 2011-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-02
        • 1970-01-01
        相关资源
        最近更新 更多