单击DataColumn时更改DataGridTemplateColumn WPF

问题描述

在WPF文档中,我有一个DataGrid

<DataGrid>
    <DataGrid.ColumnHeaderStyle >
        <Style BasedOn="{StaticResource MaterialDesignDataGridColumnHeader}" targettype="{x:Type DataGridColumnHeader}">
            <Setter Property="FontSize" Value="11"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
    </DataGrid.ColumnHeaderStyle>
    <DataGrid.RowStyle>
        <Style BasedOn="{StaticResource MaterialDesignDataGridRow}" targettype="{x:Type DataGridRow}">
            <Style.Triggers>
                <Trigger Property="DataGridCell.IsSelected" Value="True">
                    <Setter Property="Background" Value="Blue" />
                    <Setter Property="BorderBrush" Value="Blue" />
                </Trigger>
                <Trigger Property="IsMouSEOver" Value="True">
                    <Setter Property="Background" Value="Blue" />
                </Trigger>
                <Trigger Property="IsMouSEOver" Value="False">
                    <Setter Property="Background" Value="Red" />
                </Trigger>
            </Style.Triggers>
            <Setter Property="ContextMenu" Value="{StaticResource TasksMenu}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate targettype="{x:Type DataGridRow}">
                        <Border x:Name="DGR_Border" BorderBrush="Transparent" BorderThickness="0" Margin="0,15,0" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" Height="50" CornerRadius="10">
                            <Grid Height="auto" Width="auto">
                                <SelectiveScrollingGrid>
                                    <SelectiveScrollingGrid.ColumnDeFinitions>
                                        <ColumnDeFinition Width="Auto"/>
                                        <ColumnDeFinition Width="*"/>
                                    </SelectiveScrollingGrid.ColumnDeFinitions>
                                    <SelectiveScrollingGrid.RowDeFinitions>
                                        <RowDeFinition Height="*"/>
                                        <RowDeFinition Height="Auto"/>
                                    </SelectiveScrollingGrid.RowDeFinitions>
                                    <DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" FontSize="5"/>
                                    <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen,ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical},Converter={x:Static DataGrid.RowDetailsScrollingConverter},RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
                                    <DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" FontSize="5" Visibility="{Binding HeadersVisibility,ConverterParameter={x:Static DataGridHeadersVisibility.Row},Converter={x:Static DataGrid.HeadersVisibilityConverter},RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                </SelectiveScrollingGrid>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.RowStyle>
    <DataGrid.Columns>
        <DataGridTemplateColumn x:Name="THISISTHECOLUMN" Width="*" MaxWidth="60">
            <DataGridTemplateColumn.HeaderStyle>
                <Style BasedOn="{StaticResource MaterialDesignDataGridColumnHeader}" targettype="{x:Type DataGridColumnHeader}">
                    <Setter Property="HorizontalAlignment" Value="Left" />
                    <Setter Property="FontSize" Value="11"/>
                </Style>
            </DataGridTemplateColumn.HeaderStyle>
            <DataGridTemplateColumn.CellStyle>
                <Style targettype="{x:Type DataGridCell}">
                    <Style.Triggers>
                        <Trigger Property="DataGridCell.IsSelected" Value="True">
                            <Setter Property="Background" Value="Red" />
                            <Setter Property="BorderBrush" Value="Red" />
                        </Trigger>
                    </Style.Triggers>
                    <Setter Property="Padding" Value="20"/>
                    <!--<Setter Property="Height" Value="57"/>-->
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate targettype="DataGridCell">
                                <Border Margin="2" BorderThickness="2" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" CornerRadius="5">
                                    <ContentPresenter />
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <!--<Setter Property="BorderBrush"><Setter.Value><MultiBinding><MultiBinding.Converter><local:CellForegroundMultiValueConverter /></MultiBinding.Converter><Binding RelativeSource="{RelativeSource Self}" Path="Column.Header"/><Binding/><Binding Path="HasChanges" /></MultiBinding></Setter.Value></Setter>-->
                </Style>
            </DataGridTemplateColumn.CellStyle>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Border Background="Blue" Height="40" CornerRadius="10" BorderBrush="Transparent">
                        <TextBlock Margin="5 0" FontSize="15" Width="auto" Text="1" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Yellow"/>
                    </Border>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Header="Header 1" Binding="{Binding Header1,UpdateSourceTrigger=PropertyChanged}">
            <DataGridTextColumn.ElementStyle>
                <Style targettype="TextBlock">
                    <Setter Property="FontSize" Value="8" />
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Header="Header 2" Binding="{Binding Header2,UpdateSourceTrigger=PropertyChanged}">
            <DataGridTextColumn.ElementStyle>
                <Style targettype="TextBlock">
                    <Setter Property="FontSize" Value="8" />
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Header="Header 3" Binding="{Binding Header3,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
            <DataGridTextColumn.ElementStyle>
                <Style targettype="TextBlock">
                    <Setter Property="FontSize" Value="8" />
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
    <DataGrid.CellStyle>
        <Style targettype="{x:Type DataGridCell}">
            <Style.Triggers>
                <Trigger Property="DataGridCell.IsSelected" Value="True">
                    <Setter Property="Background" Value="Blue" />
                    <Setter Property="BorderBrush" Value="Blue" />
                </Trigger>
            </Style.Triggers>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Padding" Value="20"/>
            <Setter Property="FontSize" Value="9"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate targettype="{x:Type DataGridCell}">
                        <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Foreground">
                <Setter.Value>
                    <MultiBinding>
                        <MultiBinding.Converter>
                            <local:CellForegroundMultiValueConverter />
                        </MultiBinding.Converter>
                        <Binding RelativeSource="{RelativeSource Self}" Path="Column.Header"/>
                        <Binding/>
                        <Binding Path="HasChanges" />
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.CellStyle>
</DataGrid>

在按钮悬停时,DataGridColumn完全从红色变为蓝色,但是当我单击它时,第一个DataGridTemplateColumn标记THISISTHECOLUMN)不会改变颜色。我该如何做才能使单击主DataGridColumn时它也改变颜色?

这是到目前为止的样子:

enter image description here

解决方法

DataGridRow样式添加触发器,以将选择时的DGR_Border边框颜色更改为Blue

<Style BasedOn="{StaticResource MaterialDesignDataGridRow}" TargetType="{x:Type DataGridRow}">
   <Style.Triggers>
      <!-- ...your other triggers. -->
      <Trigger Property="IsSelected" Value="True">
         <Setter Property="Background" Value="Blue" />
      </Trigger>
   </Style.Triggers>
   <!-- ...rest of the style. -->
</Style>

更改DataGridCell触发器,以设置Blue BackgroundBorderBrush而不是Red

<Style TargetType="{x:Type DataGridCell}">
   <Style.Triggers>
      <Trigger Property="DataGridCell.IsSelected" Value="True">
         <Setter Property="Background" Value="Blue" />
         <Setter Property="BorderBrush" Value="Blue" />
      </Trigger>
   </Style.Triggers>
   <!-- ...your other setters. -->
</Style>