我需要将某些WPF Datagrid行显示为只读或不显示,具体取决于绑定模型上的属性。
如何做到这一点?
答案 0 :(得分:21)
我遇到了同样的问题。 使用jsmith的答案和Nigel Spencer的博客中提供的信息,我提出了一个解决方案,不需要更改WPF DataGrid源代码,子类化或添加代码来查看代码隐藏。如您所见,我的解决方案非常适合MVVM。
它使用Expression Blend Attached Behavior mechanism,因此您需要安装Expression Blend SDK并添加对Microsoft.Expression.Interactions.dll的引用,但如果您不这样做,此行为可以轻松转换为native attached behavior像那样。
<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>
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
}
}
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;
}
}
}
答案 1 :(得分:13)
我找到了几个解决这个问题的简单方法。我认为最好的是连接到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>
答案 2 :(得分:3)
我认为最简单的方法是向DataGridRow类添加一个IsReadOnly属性。 Nigel Spencer有一篇关于如何做到这一点的详细文章here。