日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

WPF Datagrid with some read-only rows - Stack Overflow

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

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