原文: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:

<DataGrid
xmlns:Behaviors="clr-namespace:My.Common.Behaviors"
...
>
<i:Interaction.Behaviors>
<Behaviors:DataGridRowReadOnlyBehavior/>
</i:Interaction.Behaviors>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsReadOnly}" Value="True"/>
<Setter Property="Behaviors:ReadOnlyService.IsReadOnly" Value="True"/>
<Setter Property="Foreground" Value="LightGray"/>
<Setter Property="ToolTipService.ShowOnDisabled" Value="True"/>
<Setter Property="ToolTip" Value="Disabled in ViewModel"/>
</DataTrigger> </Style.Triggers>
</Style>
</DataGrid.Resources>
...
</DataGrid>

ReadOnlyService.cs

using System.Windows;

namespace My.Common.Behaviors
{
internal class ReadOnlyService : DependencyObject
{
#region IsReadOnly /// <summary>
/// IsReadOnly Attached Dependency Property
/// </summary>
private static readonly DependencyProperty BehaviorProperty =
DependencyProperty.RegisterAttached("IsReadOnly", typeof(bool), typeof(ReadOnlyService),
new FrameworkPropertyMetadata(false)); /// <summary>
/// Gets the IsReadOnly property.
/// </summary>
public static bool GetIsReadOnly(DependencyObject d)
{
return (bool)d.GetValue(BehaviorProperty);
} /// <summary>
/// Sets the IsReadOnly property.
/// </summary>
public static void SetIsReadOnly(DependencyObject d, bool value)
{
d.SetValue(BehaviorProperty, value);
} #endregion IsReadOnly
}
}

DataGridRowReadOnlyBehavior.cs

using System;
using System.Windows.Controls;
using System.Windows.Interactivity; namespace My.Common.Behaviors
{
/// <summary>
/// Custom behavior that allows for DataGrid Rows to be ReadOnly on per-row basis
/// </summary>
internal class DataGridRowReadOnlyBehavior : Behavior<DataGrid>
{
protected override void OnAttached()
{
base.OnAttached();
if (this.AssociatedObject == null)
throw new InvalidOperationException("AssociatedObject must not be null"); AssociatedObject.BeginningEdit += AssociatedObject_BeginningEdit;
} private void AssociatedObject_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
var isReadOnlyRow = ReadOnlyService.GetIsReadOnly(e.Row);
if (isReadOnlyRow)
e.Cancel = true;
} protected override void OnDetaching()
{
AssociatedObject.BeginningEdit -= AssociatedObject_BeginningEdit;
}
}
}

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. sequence配置

    在实现分库分表的情况下,数据库自增主键已经无法保证在集群中是全局唯一的主键了,因此mycat提供了全局的sequence,并且提供了本地配置.数据库配置等多种实现方式. 本地文件方式 采用该方式,my ...

  2. ssh登录很慢,登录上去后速度正常问题的解决方法

    1. DNS反向解析的问题 OpenSSH在用户登录的时候会验证IP,它根据用户的IP使用反向DNS找到主机名,再使用DNS找到IP地址,最后匹配一下登录的IP是否合法.如果客户机的IP没有域名,或者 ...

  3. [Angular] Subscribing to the valueChanges Observable

    For example we have built a form: form = this.fb.group({ store: this.fb.group({ branch: '', code: '' ...

  4. js如何实现页面跳转(大全)

    js如何实现页面跳转(大全) 一.总结 一句话总结: 1.location的href属性: js跳转主要是通过window的location对象的href属性,因为location对象本来就是表示的浏 ...

  5. C++生成GIF小结

    声明:所有权利保留. 转载必须说明出处:http://blog.csdn.net/cartzhang/article/details/44020175 近来需要把BMP或Kinect的内存图片多张合成 ...

  6. 【hdu 3863】No Gambling

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65568/32768 K (Java/Others) Total Submission(s) ...

  7. Tor (洋葱头)torbrowser

    Tor是什么 Tor是互联网上用于保护您隐私最有力的工具之一,但是时至今日仍有许多人往往认为Tor是一个终端加密工具.事实上,Tor是用来匿名浏览网页和邮件发送(并非是邮件内容加密)的.今天,我们要讨 ...

  8. Oracle 11g对依赖的推断达到字段级

    在Oracle 10g下,推断依赖性仅仅达到了对象级.也就是说存储过程訪问的对象一旦发生了变化.那么Oracle就会将存储过程置为INVALID状态.所以在为表做了DDL操作后.须要把存储过程又一次进 ...

  9. TCP协议的一些认识及实践

    http://www.2cto.com/net/201210/163047.html 一.简介 引用<TCP/IP详解-卷1>中的介绍,TCP与UDP使用相同的网络层(IP层),TCP却向 ...

  10. [Android]TextView实现分散对齐(两端对齐)

    TextView是个特别基础的Android控件,只要有文本基本就少不了它.但是最近在项目开发的过程中我发现TextView存在很多局限性,其中最令我头疼的就是TextView文本排版方面的问题.我们 ...