C# 基于行项值的wpf datagrid单元格样式

C# 基于行项值的wpf datagrid单元格样式,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,这应该是相对容易做到的,但由于某种原因,我没有得到它的工作。我有一个类ViewWrapper,它有一个属性ViewGroup和ViewGroupEditable。我的数据网格中有一列绑定到ViewGroup属性,现在如果属性ViewGroupEditable为false,我想禁用对该单元格的编辑。因此,我创建了一个带有简单数据触发器的DataGridCell样式,但它什么也不做。我错过了什么 XAML: <UserControl x:Class="GrimshawRibbon.Revit.

这应该是相对容易做到的,但由于某种原因,我没有得到它的工作。我有一个类
ViewWrapper
,它有一个属性
ViewGroup
ViewGroupEditable
。我的数据网格中有一列绑定到
ViewGroup
属性,现在如果属性
ViewGroupEditable
为false,我想禁用对该单元格的编辑。因此,我创建了一个带有简单数据触发器的
DataGridCell
样式,但它什么也不做。我错过了什么

XAML:

<UserControl x:Class="GrimshawRibbon.Revit.Views.ViewManager.ViewManagerView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:local="clr-namespace:GrimshawRibbon.Revit.Views.ViewManager"
             xmlns:ex="clr-namespace:GrimshawRibbon.Revit.Wpf.Extensions"
             xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
             xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="500">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
                <ResourceDictionary Source="pack://application:,,,/GrimshawRibbon;component/Revit/Wpf/Style/GrimshawTheme.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <Grid.Resources>
        <Style x:Key="dgCellStyle" TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ViewGroupEditable}" Value="True">
                    <Setter Property="IsEnabled" Value="False"/>
                    <Setter Property="Foreground" Value="LightGray"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        </Grid.Resources>
        <ex:DataGridEx x:Name="dgViews" 
                               Style="{StaticResource AzureDataGrid}" 
                               Margin="10" 
                               AutoGenerateColumns="False" 
                               ItemsSource="{Binding Views, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                               CanUserAddRows="False" 
                               IsReadOnly="False" 
                               SelectionMode="Extended" 
                               SelectionUnit="FullRow" 
                               SelectedItem="{Binding SelectedRow, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                               SelectedItemsList="{Binding SelectedViews, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="CellEditEnding">
                    <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding CellEditEndingCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
                <DataGridTextColumn Header="ViewType" Binding="{Binding ViewType}" Width="100" IsReadOnly="True"/>
                <DataGridTextColumn Header="ViewGroup" Binding="{Binding ViewGroup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="130" CellStyle="{StaticResource dgCellStyle}"/>
                <DataGridTextColumn Header="ViewSubGroup" Binding="{Binding ViewSubGroup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="130" IsReadOnly="False"/>
                <DataGridCheckBoxColumn ElementStyle="{DynamicResource MetroDataGridCheckBox}" 
                                        EditingElementStyle="{DynamicResource MetroDataGridCheckBox}" 
                                        Header="OnSheet" 
                                        Binding="{Binding OnSheet, Mode=TwoWay}" 
                                        IsReadOnly="True" 
                                        Width="80">
                </DataGridCheckBoxColumn>
                <DataGridTemplateColumn Header="ViewTemplate" Width="180">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding ViewTemplate.Name}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding DataContext.ViewTemplates, RelativeSource={RelativeSource AncestorType=DataGrid}}"
                                      SelectedItem="{Binding ViewTemplate}"
                                      DisplayMemberPath="Name">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="SelectionChanged">
                                        <cmd:EventToCommand PassEventArgsToCommand="True"
                                                            Command="{Binding DataContext.SelectChangeCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </ex:DataGridEx>
    </Grid>
</UserControl>

DataGridTextColumn
没有
IsEnabled
属性,因此无法设置。您可以改为尝试
IsReadOnly
(请注意,您将禁用整个列)

您可能会更幸运地使用这种方法:

 <DataGridTextColumn Width="80" Header="My column header" Binding="{Binding Path=MyProperty, Mode=TwoWay}">
    <DataGridTextColumn.EditingElementStyle>
       <Style TargetType="TextBox">
           <Setter Property="IsEnabled" Value="{Binding IsEditingEnabled}"/>
       </Style>
    </DataGridTextColumn.EditingElementStyle>
  </DataGridTextColumn>

我能看到的唯一问题是
ViewWrapper
没有实现
INotifyPropertyChanged
。您应该在所有属性中使用类似的内容:

public class ViewWrapper : ElementWrapper, INotifyPropertyChanged
{
    string _viewSubGroup;
    public string ViewSubGroup { get { return _viewSubGroup; } set { _viewSubGroup = value; RaisePropertyChanged("ViewSubGroup"); } } 

    public event PropertyChangedEventHandler PropertyChanged;
    void RaisePropertyChanged(string propname)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propname));
    }
}

请参阅许多其他类。

它继承的
ElementWrapper
类实现了这一点。它是一个
observateObject
类。即使如此,您仍然需要
RaisePropertyChanged(“ViewSubGroup”)(或类似的东西)来引发事件。事实上,我没有注意到这一点。很好的建议。非常感谢。看来我的数据设置器正在工作。不正常的是我的背景色。选中行时,它将变为深蓝色。这与你所说的textbox有关吗?@konrad这是默认的选择颜色,应该对你有帮助
 <Style x:Key="dgCellStyle" TargetType="{x:Type TextBox}"">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ViewGroupEditable}" Value="True">
                <Setter Property="IsEnabled" Value="False"/>
                <Setter Property="Foreground" Value="LightGray"/>
            </DataTrigger>
        </Style.Triggers>
  </Style>

  <DataGridTextColumn Header="ViewGroup" 
       Binding="{Binding ViewGroup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
       Width="130" 
       EditingElementStyle="{StaticResource dgCellStyle}"/>
public class ViewWrapper : ElementWrapper, INotifyPropertyChanged
{
    string _viewSubGroup;
    public string ViewSubGroup { get { return _viewSubGroup; } set { _viewSubGroup = value; RaisePropertyChanged("ViewSubGroup"); } } 

    public event PropertyChangedEventHandler PropertyChanged;
    void RaisePropertyChanged(string propname)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propname));
    }
}