我一直在努力这一段时间了。我在我的应用程序中有一个Master/Details布局,和其他许多人一样,DataGrid在禁用它时遇到了问题。 Essencialy在从列表中选择一个元素以填充一系列字段后,用户按下“Edit”,这将禁用DataGrid并启用所有表单的字段。按下“保存”按钮将在保存数据后恢复这些操作...相当海峡向前。WPF DataGrid - 禁用时保留选择
我在Windows 7中与2010年深化发展VS .NET Framework 4的
我曾尝试:
1)基于this post,我曾尝试使用DataGrid在2009年6月版WPF工具包,但我有同样的反应。
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,这仍然取消选择当前选定的行。我试图interprete的最新留言(在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
?
非常感谢!
THX战士。叠加的问题是,即使通过放置DataGrid Focusable = False,AllowKeyboardFocus = False,用户仍然可以使用该选择(按下键盘上的向上和向下箭头,出于某种原因导致焦点转到下一个直到它触及DataGrid,然后开始滚动它)。我意识到,行细节模式更受鼓励,但我不是UI设计师,像这样的设计限制不会让设计师感到高兴! ;-) – JFTxJ
尝试设置IsHitTestVisible = False。用户不应该能够影响它。 – CodeWarrior