带有一些只读行的 WPF Datagrid [英] WPF Datagrid with some read-only rows

查看:19
本文介绍了带有一些只读行的 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.

如何做到这一点?

推荐答案

我遇到了同样的问题.使用 jsmith 的回答和 Nigel Spencer 的博客中提供的信息,我提出了一个解决方案,不需要更改 WPF DataGrid 源代码、子类化或添加代码以查看视图的代码隐藏.如您所见,我的解决方案对 MVVM 非常友好.

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.

它使用表达式混合附加行为机制 因此您需要安装 Expression Blend SDK 并添加对 Microsoft.Expression.Interactions.dll 的引用,但此行为可以轻松转换为 本机附加行为 如果您不喜欢那样.

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.

<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;
        }
    }
}

这篇关于带有一些只读行的 WPF Datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆