c# - WPF DataGrid - 禁用时保留选择

标签 c# wpf datagrid selection

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

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

我尝试过的:
1)基于this post ,我曾尝试使用WPF Toolkit 2009年6月版中的DataGrid,但我也有同样的 react 。
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,这仍然会取消选择当前选定的行。我尝试像这样解释最后的评论(在 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);
    }
}  

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

首先,我的做法正确吗?考虑到取消选择是由该方法引起的,我可以完全替换我自己的方法对“OnIsEnabledChanged”的内部调用吗? 我还有其他方法可以解决这个问题吗? 或者更具体地说,我如何停止对此方法的基本版本的调用,因为它不是覆盖,因此我无法“不”调用 base.OnIsEnabledChanged

非常感谢!

最佳答案

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

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.0 安装中,未安装 4.5。
它在 .net 4.5 中已“修复”,即使对于面向 4.0 的应用程序也是如此 (“4.5 是一个 in-place upgrade ”场景/misery )。

问候,
科恩

关于c# - WPF DataGrid - 禁用时保留选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6610967/

相关文章:

c# - 如何在不使用 smtp 的情况下通过交换服务器发送电子邮件?

c# - Visual Studio 中多次点击次数的条件断点

c# - Visual Studio 扩展性 - ProjectInfo.Document 始终为 null

c# - WPF异常在另一个类中运行动画

具有多个图像的 WPF DataGrid RowDetailsTemplate (MVVM)

c# - 我需要退订吗?

c# - 当 Windows XP 将屏幕淡化为灰色时如何通知我?

wpf - 将列表框项索引作为参数传递给 objectdataprovider

c# - 更改 DataGrid(WPF) 行中的单元格正在更改下方行中的单元格

C#、WPF、将 List<string> 绑定(bind)到 DataGrid