日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

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

asp.net

WPF Datagrid with some read-only rows - Stack Overflow

發布時間:2025/6/17 asp.net 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。