具有一些只读行的 WPF Datagrid
我需要将一些 WPF Datagrid 行显示为只读或不显示,具体取决于我绑定模型上的属性。
这怎么能做到呢?
I have the need to show some of my WPF Datagrid rows as read only or not depending on a property on my bound model.
How can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也有同样的问题。
使用 jsmith 的答案和 Nigel Spencer 的博客中提供的信息,我提出了一个解决方案,不需要更改 WPF DataGrid 源代码、子类化或添加代码以查看代码隐藏。正如您所看到的,我的解决方案非常 MVVM 友好。
它使用表达式混合附加行为机制因此,您需要安装 Expression Blend SDK 并添加对 Microsoft.Expression.Interactions.dll 的引用,但此行为可以轻松转换为 本机附加行为(如果您不喜欢的话)。
用法:
ReadOnlyService.cs
DataGridRowReadOnlyBehavior.cs
I had the same problem.
Using information provided in jsmith's answer and on Nigel Spencer's blog, I've come up with a solution that doesn't require changing WPF DataGrid source code, subclassing or adding code to view's codebehind. As you can see, my solution is very MVVM Friendly.
It uses Expression Blend Attached Behavior mechanism so you'll need to install Expression Blend SDK and add reference to Microsoft.Expression.Interactions.dll, but this behavior could be easily converted to native attached behavior if you don't like that.
Usage:
ReadOnlyService.cs
DataGridRowReadOnlyBehavior.cs
我找到了解决这个问题的几个简单方法。我认为最好的方法是连接到 DataGrid 的 BeginningEdit 事件。这与 Nigel Spencer 在他的文章中所做的类似,但您不必从 DataGrid 覆盖它。这个解决方案非常棒,因为它不允许用户编辑该行中的任何单元格,但它允许用户选择该行。
在代码隐藏中:
在 XAML 中:
不同的解决方案...这根本不允许用户选择该行,但不需要在代码隐藏中添加其他代码。
I found a couple of simple solutions to this problem. The best in my opinion was hooking up to the BeginningEdit event of the DataGrid. This is similar to what Nigel Spencer did in his post, but you don't have to override it from DataGrid. This solution is great since it doesn't allow the user to edit any of the cells in that row, but it does allow them to select the row.
In Code Behind:
In XAML:
Different Solution... This does not allow the user to select the row at all, but does not require additional code in the code behind.
我认为最简单的方法是将 IsReadOnly 属性添加到 DataGridRow 类。 Nigel Spencer 在此处撰写了一篇详细文章,介绍了如何执行此操作。
I think the easiest way to do it is to add an IsReadOnly property to the DataGridRow class. There is a detailed article by Nigel Spencer on how to do this here.