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

更改DataGrid WPF中禁用单元格的背景色,wpf,xaml,datagrid,wpf-style,Wpf,Xaml,Datagrid,Wpf Style,我有一个数据网格控件,它将禁用的单元格与完全启用的单元格组合在一起(有些单元格有下拉列表、文本框和复选框)。问题是禁用单元格的样式与启用单元格的样式完全相同。我只是想更改所有禁用单元格的样式,这样用户就可以清楚地看到,他们无法更改数据。以下是我的XAML代码: <DataGrid Name="DataGrid" ItemsSource="{Binding MySource}" AutoGenerateColumns="False" Grid.Row="1" Bor

我有一个数据网格控件,它将禁用的单元格与完全启用的单元格组合在一起(有些单元格有下拉列表、文本框和复选框)。问题是禁用单元格的样式与启用单元格的样式完全相同。我只是想更改所有禁用单元格的样式,这样用户就可以清楚地看到,他们无法更改数据。以下是我的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>


我将触发器移动到DataGridRow样式,没有骰子。对不起,我看到的是“这对我不起作用”的部分,但随后我再次阅读了整个问题。所以,不起作用的是被禁用的单元格应该有一个粉红色的背景,对吗?我只是设置了另一个触发器来禁用某些单元格,它们会有粉红色的背景。如何禁用单元格?我正在对
DataGrid\u beginingedit
事件(如果适用)设置
e.Cancel=true
,并逐行处理。我想这会破坏引擎盖下的细胞。XAML样式中是否有处理此问题的属性?啊,好的,您所做的与禁用无关
DataGridCell
有一个
IsEnabled
属性。要禁用单元格,请将其设置为false。我给了我的行一个
IsOdd
属性,并编写了这样一个触发器: