原文:WPF Datagrid with some read-only rows - Stack Overflow

up vote
21
down vote

accepted

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:

  1. <DataGrid
  2. xmlns:Behaviors="clr-namespace:My.Common.Behaviors"
  3. ...
  4. >
  5. <i:Interaction.Behaviors>
  6. <Behaviors:DataGridRowReadOnlyBehavior/>
  7. </i:Interaction.Behaviors>
  8. <DataGrid.Resources>
  9. <Style TargetType="{x:Type DataGridRow}">
  10. <Style.Triggers>
  11. <DataTrigger Binding="{Binding IsReadOnly}" Value="True"/>
  12. <Setter Property="Behaviors:ReadOnlyService.IsReadOnly" Value="True"/>
  13. <Setter Property="Foreground" Value="LightGray"/>
  14. <Setter Property="ToolTipService.ShowOnDisabled" Value="True"/>
  15. <Setter Property="ToolTip" Value="Disabled in ViewModel"/>
  16. </DataTrigger>
  17. </Style.Triggers>
  18. </Style>
  19. </DataGrid.Resources>
  20. ...
  21. </DataGrid>

ReadOnlyService.cs

  1. using System.Windows;
  2. namespace My.Common.Behaviors
  3. {
  4. internal class ReadOnlyService : DependencyObject
  5. {
  6. #region IsReadOnly
  7. /// <summary>
  8. /// IsReadOnly Attached Dependency Property
  9. /// </summary>
  10. private static readonly DependencyProperty BehaviorProperty =
  11. DependencyProperty.RegisterAttached("IsReadOnly", typeof(bool), typeof(ReadOnlyService),
  12. new FrameworkPropertyMetadata(false));
  13. /// <summary>
  14. /// Gets the IsReadOnly property.
  15. /// </summary>
  16. public static bool GetIsReadOnly(DependencyObject d)
  17. {
  18. return (bool)d.GetValue(BehaviorProperty);
  19. }
  20. /// <summary>
  21. /// Sets the IsReadOnly property.
  22. /// </summary>
  23. public static void SetIsReadOnly(DependencyObject d, bool value)
  24. {
  25. d.SetValue(BehaviorProperty, value);
  26. }
  27. #endregion IsReadOnly
  28. }
  29. }

DataGridRowReadOnlyBehavior.cs

  1. using System;
  2. using System.Windows.Controls;
  3. using System.Windows.Interactivity;
  4. namespace My.Common.Behaviors
  5. {
  6. /// <summary>
  7. /// Custom behavior that allows for DataGrid Rows to be ReadOnly on per-row basis
  8. /// </summary>
  9. internal class DataGridRowReadOnlyBehavior : Behavior<DataGrid>
  10. {
  11. protected override void OnAttached()
  12. {
  13. base.OnAttached();
  14. if (this.AssociatedObject == null)
  15. throw new InvalidOperationException("AssociatedObject must not be null");
  16. AssociatedObject.BeginningEdit += AssociatedObject_BeginningEdit;
  17. }
  18. private void AssociatedObject_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
  19. {
  20. var isReadOnlyRow = ReadOnlyService.GetIsReadOnly(e.Row);
  21. if (isReadOnlyRow)
  22. e.Cancel = true;
  23. }
  24. protected override void OnDetaching()
  25. {
  26. AssociatedObject.BeginningEdit -= AssociatedObject_BeginningEdit;
  27. }
  28. }
  29. }

answered Jan 12 '12 at 0:02
surfen

4,03412443

  • Thank you.The right answer is here. it worked for me like a charm.
    – Mohsen
    Jul 8 '12 at 6:04

  • 2

    You need the IsReadOnly property somewhere to make this work so I added an interface and casted e.Row.Item to it, making the ReadOnlyService unnecessary, IMO. Big +1 on this though. Cheers
    – Berryl
    Oct 14 '12 at 20:55

WPF Datagrid with some read-only rows - Stack Overflow的更多相关文章

  1. WPF DataGrid显格式

    Guide to WPF DataGrid formatting using bindings Peter Huber SG, 25 Nov 2013 CPOL    4.83 (13 votes) ...

  2. WPF DataGrid常用属性记录

    WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭 ...

  3. WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL

    In my recent codeproject article on the DataGrid I described a number of techniques for handling the ...

  4. WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.

    WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次  悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...

  5. xceed wpf datagrid

    <!--*********************************************************************************** Extended ...

  6. 获取wpf datagrid当前被编辑单元格的内容

    原文 获取wpf datagrid当前被编辑单元格的内容 确认修改单元个的值, 使用到datagrid的两个事件 开始编辑事件 BeginningEdit="dataGrid_Beginni ...

  7. WPF DataGrid绑定一个组合列

    WPF DataGrid绑定一个组合列 前台: <Page.Resources>        <local:InfoConverter x:Key="converter& ...

  8. WPF DataGrid自定义样式

    微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师).下面,找到我的小抄造型的网格.它不是100%全面,但它可以让你走得很远,有一些非常有用的技巧和陷阱. ...

  9. WPF DataGrid Custommization using Style and Template

    WPF DataGrid Custommization using Style and Template 代码下载:http://download.csdn.net/detail/wujicai/81 ...

随机推荐

  1. C运行时库(C Run-time Library)详解(提供的另一个最重要的功能是为应用程序添加启动函数。Visual C++对控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)

    一.什么是C运行时库 1)C运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些库中的函数. 2)C 语言是所谓的“ ...

  2. js中单引号和双引号的区别(html中属性规范是用双引号,js中字符串规定是用单引号)(js中单引号区别和php很像:单引号快,双引号可转义字符,双引号可解析变量)

    js中单引号和双引号的区别(html中属性规范是用双引号,js中字符串规定是用单引号)(js中单引号区别和php很像:单引号快,双引号可转义字符,双引号可解析变量) 一.总结 1.html中属性规范是 ...

  3. mysqldump备份脚本

    #!/bin/bash # 10 23 * * * /bin/bash /data/script/backup_mysqldump.sh BDATE=`date +%Y%m%d%H%M%S`BPATH ...

  4. VC++ 訪问数据库实例具体解释图解

    一 ADO 方式訪问 Access 新建一个对话框project,加入控件,如图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2 ...

  5. <a href='javacript:' title='{$str}'>是什么意思(多看学习视频)

    <a href='javacript:' title='{$str}'>是什么意思(多看学习视频) 一.总结 一句话总结: 1.javascript:是什么? 伪协议,后面接javascr ...

  6. MONyog使用图解(一)-数据库性能监控工具

    原文:MONyog使用图解(一)-数据库性能监控工具 一.安装步骤 较为简单,网上可以搜索到,此处不做详细说明. 二.使用图解 此处介绍监控数据库连接量.并发量.吞吐量.响应时间等功能 1.设置连接需 ...

  7. 关于在方法里面使用泛型public static <T> T

    对泛型的理解一直处于模糊的状态,所以今天就专门看了看泛型,看到了如下的一段代码,网上没有找到相应的解释. 然后尝试着自己理解了一下,第一次写博客,手比较生. 现在就开始来写我的理解. 看到这个方法的第 ...

  8. Leetcode--easy系列1

    近期開始刷Leetcode题目.花了一个星期先完毕了easy难度级别的题目,easy级别的题目思路比較简单,但不一定就直接AC了,主要是问题要考虑全然.特别是对特殊情况的处理. #6 ZigZag C ...

  9. Linux下安装mysql(1)(CentOS)

    标题是(1)也就是说这次是基础安装,这种方式安装,没有组的创建,权限管理,配置文件更改等,仅仅是最基本的安装,适合第一次在linux上安装mysql的新手 1.准备好安装包(Linux-Generic ...

  10. CodeBlocks提供了预编译的WxWidgets模块,并预置TDM

    Miscellaneous For Windows, we also provide the pre-compiled wxWidgets, version 2.8.12 used to compil ...