原文: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. 浅谈java中异常抛出后代码是否会继续执行

    问题 今天遇到一个问题,在下面的代码中,当抛出运行时异常后,后面的代码还会执行吗,是否需要在异常后面加上return语句呢? public void add(int index, E element) ...

  2. 转载:使用bat命令来快速安装和卸载Service服务

    一般我们在编写完Service服务程序后,都是通过cmd命令提示窗口来安装或卸载服务,但频繁的在cmd窗口中去“拼”文件的路径着实让人“不能忍”.所以,我们需要一钟“更快捷”的方式来进行安装或者卸载操 ...

  3. Android 控件EditText的setOnEditorActionListener方法的理解

    需要注意的是 setOnEditorActionListener这个方法,并不是在我们点击EditText的时候触发,也不是在我们对EditText进行编辑时触发,而是在我们编辑完之后点击软键盘上的回 ...

  4. css3-2 CSS3选择器和文本字体样式

    css3-2   CSS3选择器和文本字体样式 一.总结 一句话总结:是要记下来的,记下来可以省很多事. 1.css的基本选择器中的:first-letter和:first-line是什么意思? :f ...

  5. 【icpc网络赛大连赛区】Sparse Graph

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submissi ...

  6. 【u032】均衡发展

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 神牛小R在许多方面都有着很强的能力,具体的说,他总共有m种能力,并将这些能力编号为1到m.他的能力是一 ...

  7. hdu 1292 &quot;下沙野骆驼&quot;ACM夏令营 (递推)

    "下沙野骆驼"ACM夏令营 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/ ...

  8. 【hdu 3537】Daizhenyang's Coin

    Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  9. OSGi开发环境的建立

    1 OSGi开发环境的建立 1.1 Equinox是什么 从代码角度来看,Equinox其实就是OSGi核心标准的完整实现,并且还在这个基础上增加了一些额外的功能(比如为框架增加了命令行和程序执行的入 ...

  10. 静态编译ltrace

    ltrace可以跟踪进程的库函数调用,它会显现出哪个库函数被调用,而strace则是跟踪程序的每一个系统调用. 有时候只使用strace还是不够的,须要ltrace配合才干找出问题出在哪里. 假设在b ...