WPF Datagrid with some read-only rows - Stack Overflow

简介: 原文:WPF Datagrid with some read-only rows - Stack Overflow up vote 21 down vote ...
原文: 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;
        }
    }
}
  • 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
目录
相关文章
|
2月前
|
前端开发 C#
浅谈WPF之DataGrid动态生成列
在日常开发中,DataGrid作为二维表格,非常适合数据的展示和统计。通常情况下,一般都有固定的格式和确定的数据列展示,但是在某些特殊情况下,也可能会需要用到动态生成列。本文以一些简单的小例子,简述在WPF开发中,如何动态生成DataGrid的行和列,仅供学习分享使用,如有不足之处,还请指正。
99 2
|
8月前
|
数据挖掘 数据处理 C#
WPF技术之DataGrid控件
WPF DataGrid是一种可以显示和编辑数据的界面控件。它可以作为表格形式展示数据,支持添加、删除、修改、排序和分组操作。
174 0
|
9月前
|
C#
让WPF中的DataGrid像Excel一样可以筛选(下)
让WPF中的DataGrid像Excel一样可以筛选(下)
125 0
|
9月前
|
前端开发 C# 数据库
让WPF中的DataGrid像Excel一样可以筛选(上)
让WPF中的DataGrid像Excel一样可以筛选(上)
115 0
让WPF中的DataGrid像Excel一样可以筛选(上)
|
9月前
|
C# 数据库
WPF中DataGrid控件绑定数据源
WPF中DataGrid控件绑定数据源
115 0
WPF 点击 Datagrid 中的TextBox 控件获取其所在行的数据
WPF 点击 Datagrid 中的TextBox 控件获取其所在行的数据
|
大数据 C# 数据库
WPF DataGrid 性能加载大数据
原文:WPF DataGrid 性能加载大数据 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010265681/article/details/76651725  WPF(Windows Presentation Foundation)应用程序在没有图形加速设备的机器上运行速度很慢是个公开的秘密,给用户的感觉是它太吃资源了,WPF程序的性能和硬件确实有很大的关系,越高档的机器性能越有优势。
2092 0
|
C#
WPF 4 DataGrid 控件(自定义样式篇)
原文:WPF 4 DataGrid 控件(自定义样式篇)      在《WPF 4 DataGrid 控件(基本功能篇)》中我们已经学习了DataGrid 的基本功能及使用方法。本篇将继续介绍自定义DataGrid 样式的相关内容,其中将涉及到ColumnHeader、RowHeader、Row、Cell 的各种样式设置。
2462 0
|
C# 前端开发
wpf中的datagrid绑定操作按钮是否显示或者隐藏
如图,需要在wpf中的datagrid的操作那列有个确认按钮,然后在某些条件下确认按钮可见,某些情况下不可见的,放在mvc里直接在cshtml页面中if..else就行了。 但是在wpf里不行。。网上搜索了好久才找到解决方法,原来只是binding那个visiable属性就行了,
6845 0