WPF Datagrid with some read-only rows - Stack Overflow

來源:互聯網
上載者:User

標籤:you   summary   spl   users   tooltip   jsm   data   schema   ram   

原文: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;        }    }}
share|improve this answer edited Jul 9 ‘12 at 6:40 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
add a comment | 

WPF Datagrid with some read-only rows - Stack Overflow

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.