欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > asp.net >内容正文

asp.net

WPF Datagrid with some read-only rows - Stack Overflow

发布时间:2025/6/17 asp.net 49 豆豆
生活随笔 收集整理的这篇文章主要介绍了 WPF Datagrid with some read-only rows - Stack Overflow 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
原文: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的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。