具有一些只读行的 WPF Datagrid

发布于 2024-08-17 03:04:52 字数 70 浏览 3 评论 0原文

我需要将一些 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.

How can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜味拾荒者 2024-08-24 03:04:52

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

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

用法:

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

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;
        }
    }
}
请远离我 2024-08-24 03:04:52

我找到了解决这个问题的几个简单方法。我认为最好的方法是连接到 DataGrid 的 BeginningEdit 事件。这与 Nigel Spencer 在他的文章中所做的类似,但您不必从 DataGrid 覆盖它。这个解决方案非常棒,因为它不允许用户编辑该行中的任何单元格,但它允许用户选择该行

在代码隐藏中:

private void MyList_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
  if (((MyCustomObject)e.Row.Item).IsReadOnly)  //IsReadOnly is a property set in the MyCustomObject which is bound to each row
  {
    e.Cancel = true;
  }
}

在 XAML 中:

<DataGrid ItemsSource="{Binding MyObservableCollection}"
          BeginningEdit="MyList_BeginningEdit">
  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding Name}"
                        Header="Name"/>
    <DataGridTextColumn Binding="{Binding Age}"
                        Header="Age"/>
  </DataGrid.Columns>
</DataGrid>

不同的解决方案...这根本不允许用户选择该行,但不需要在代码隐藏中添加其他代码。

<DataGrid ItemsSource="{Binding MyObservableCollection}">
  <DataGrid.Resources>
    <Style TargetType="{x:Type DataGridRow}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding IsReadOnly}"
                     Value="True" >
        <Setter Property="IsEnabled"
                Value="False" />   <!-- You can also set "IsHitTestVisble" = False but please note that this won't prevent the user from changing the values using the keyboard arrows -->
        </DataTrigger>

      </Style.Triggers>
    </Style>
  </DataGrid.Resources>

  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding Name}"
                        Header="Name"/>
    <DataGridTextColumn Binding="{Binding Age}"
                        Header="Age"/>
  </DataGrid.Columns>
</DataGrid>

I found a couple of simple solutions to this problem. The best in my opinion was hooking up to the BeginningEdit event of the DataGrid. This is similar to what Nigel Spencer did in his post, but you don't have to override it from DataGrid. This solution is great since it doesn't allow the user to edit any of the cells in that row, but it does allow them to select the row.

In Code Behind:

private void MyList_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
  if (((MyCustomObject)e.Row.Item).IsReadOnly)  //IsReadOnly is a property set in the MyCustomObject which is bound to each row
  {
    e.Cancel = true;
  }
}

In XAML:

<DataGrid ItemsSource="{Binding MyObservableCollection}"
          BeginningEdit="MyList_BeginningEdit">
  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding Name}"
                        Header="Name"/>
    <DataGridTextColumn Binding="{Binding Age}"
                        Header="Age"/>
  </DataGrid.Columns>
</DataGrid>

Different Solution... This does not allow the user to select the row at all, but does not require additional code in the code behind.

<DataGrid ItemsSource="{Binding MyObservableCollection}">
  <DataGrid.Resources>
    <Style TargetType="{x:Type DataGridRow}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding IsReadOnly}"
                     Value="True" >
        <Setter Property="IsEnabled"
                Value="False" />   <!-- You can also set "IsHitTestVisble" = False but please note that this won't prevent the user from changing the values using the keyboard arrows -->
        </DataTrigger>

      </Style.Triggers>
    </Style>
  </DataGrid.Resources>

  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding Name}"
                        Header="Name"/>
    <DataGridTextColumn Binding="{Binding Age}"
                        Header="Age"/>
  </DataGrid.Columns>
</DataGrid>
轮廓§ 2024-08-24 03:04:52

我认为最简单的方法是将 IsReadOnly 属性添加到 DataGridRow 类。 Nigel Spencer 在此处撰写了一篇详细文章,介绍了如何执行此操作。

I think the easiest way to do it is to add an IsReadOnly property to the DataGridRow class. There is a detailed article by Nigel Spencer on how to do this here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文