gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:13:50 24 4
gpt4 key购买 nike

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

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

我尝试过的:
1) 基于this post ,我曾尝试在 2009 年 6 月版的 WPF 工具包中使用 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);
}
}

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

首先,我这样做是否正确?考虑到 Deselection 是由这个方法引起的,我是否可以为我自己的方法完全替换对 '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.5 的 4.0 安装中。
它在 .net 4.5 中是“固定的”,即使是针对 4.0 的应用程序也是如此(“4.5 是一个 in-place upgrade”场景/misery)。

问候,
公园

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com