更改 DataGrid WPF 中禁用单元格的背景颜色

Change Background Color of Disabled Cell in DataGrid WPF

我有一个数据网格控件,它有禁用的单元格和完全启用的单元格(一些单元格有下拉菜单、文本框、复选框)。问题是禁用单元格的样式看起来与启用单元格完全一样。我只是想更改所有禁用单元格的样式,以便用户清楚他们无法更改数据。这是我的 XAML 代码:

<DataGrid Name="DataGrid" 
    ItemsSource="{Binding MySource}"
    AutoGenerateColumns="False" Grid.Row="1"
    BorderThickness="0"
    SelectionMode="Single" SelectionUnit="FullRow" 
    CanUserAddRows="False" CanUserDeleteRows="False" 
    CanUserReorderColumns="False" CanUserSortColumns="False"
    CanUserResizeColumns="False" CanUserResizeRows="False" 
    BeginningEdit="DataGrid_BeginningEdit" Margin="10">
    <DataGrid.Resources>
        <CollectionViewSource Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.SourceList}" x:Key="SourceChoices" />
            <CollectionViewSource Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.MyDropDownSource}" x:Key="MyDropDownOptions" />
            <CollectionViewSource Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.MySource}" x:Key="MySourceOptions" />
                <Style TargetType="DataGrid">
                    <Setter Property="GridLinesVisibility" Value="All" />
                    <Setter Property="HorizontalGridLinesBrush" Value="Gray"/>
                    <Setter Property="VerticalGridLinesBrush" Value="LightGray"/>
                    <Setter Property="FontSize" Value="13" />
                </Style>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="Background" Value="LightGray" />
                    <Setter Property="Foreground" Value="Black" />
                    <Setter Property="FontSize" Value="13" />
                    <Setter Property="FontWeight" Value="DemiBold" />
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                    <Setter Property="Height" Value="34" />
                </Style>
                <Style TargetType="DataGridCell">
                    <Setter Property="Height" Value="35" />
                    <Setter Property="Padding" Value="4" />
                    <Setter Property="BorderThickness" Value="0" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type DataGridCell}">
                                <Grid Background="{TemplateBinding Background}">
                                    <ContentPresenter VerticalAlignment="Center" />
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"/>
                    <Style.Triggers>
                        <Trigger Property="DataGridCell.IsSelected" Value="True">
                            <Setter Property="Background" Value="LightBlue" />
                            <Setter Property="Foreground" Value="Black" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Background" Value="Pink" />
                            <Setter Property="Foreground" Value="Blue" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
                <Style TargetType="DataGridRow">
                    <Setter Property="Background" Value="White" />
                    <Setter Property="BorderThickness" Value="0" />
                    <Setter Property="Width" Value="Auto" />
                </Style>
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Pos" Binding="{Binding Position}" Width="40" CanUserSort="False" />
                <DataGridTextColumn Header="Acn Nbr" Binding="{Binding MySourceNumber1}" Width="10*" CanUserSort="False" />
                <DataGridTextColumn Header="Name" Binding="{Binding MySourceNumber2}" Width="15*" CanUserSort="False" />
                <DataGridTextColumn Header="Org #" Binding="{Binding MySourceNumber3}" Width="40" CanUserSort="False" />
                <DataGridCheckBoxColumn Header="Proteus" Binding="{Binding MySourceNumber4}" Width="50" CanUserSort="False" />
                <DataGridComboBoxColumn Header="Source Id" TextBinding="{Binding MySourceNumber5}" Width="10*" CanUserSort="False" 
                                            DisplayMemberPath="Name" SelectedValuePath="ID" ItemsSource="{Binding Source={StaticResource Options}}"/>
                <DataGridComboBoxColumn Header="Bench" SelectedValueBinding="{Binding ID}" Width="10*" CanUserSort="False" 
                                            DisplayMemberPath="Name" SelectedValuePath="ID" ItemsSource="{Binding Source={StaticResource Options}}"/>
                <DataGridComboBoxColumn Header="Org Id" SelectedValueBinding="{Binding ID}" Width="10*" CanUserSort="False" 
                                            DisplayMemberPath="OrganismAbbrev" SelectedValuePath="ID" ItemsSource="{Binding Source={StaticResource Options}}"/>
                <DataGridTextColumn Header="Comment" Binding="{Binding Comment}" Width="20*" CanUserSort="False" />
            </DataGrid.Columns>
        </DataGrid>

注意代码中的部分:

<Trigger Property="DataGridCell.IsSelected" Value="True">
    <Setter Property="Background" Value="LightBlue" />
    <Setter Property="Foreground" Value="Black" />
</Trigger>

这对我不起作用。我做错了什么?

谢谢!!!

您可以将触发器放在 ControlTemplate 中。然后,无论您在触发器中指的是什么 属性,在本例中是 "IsEnabled" 或 "IsSelected",它将指向它是什么 TargetType 的 属性(在本例中DataGridCell),假设该数据类型存在这样的 属性,它将起作用。否则绑定就会中断。

 <Setter Property="Template">
        <Setter.Value>
               <ControlTemplate TargetType="{x:Type DataGridCell}">
                  <Grid Background="{TemplateBinding Background}">
                    <ContentPresenter VerticalAlignment="Center" />
                   </Grid>
               <ControlTemplate.Triggers>
                <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="LightBlue" />
                        <Setter Property="Foreground" Value="Black" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Background" Value="Pink" />
                        <Setter Property="Foreground" Value="Blue" />
                    </Trigger>
              </ControlTemplate.Triggers>
          </ControlTemplate> 
       </Setter.Value>
    </Setter>