【发布时间】: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?
非常感谢!
【问题讨论】: