动态更改 Wpf DatagridTemplateColumn 的 BorderBrush 颜色

Change BorderBrush Colour of Wpf DatagridTemplateColumn dynamically

在我的 wpf 项目中,我做了一个 IMultiValueConverter 会像这样改变我的 DataGridCells 的前景:

class CellForegroundMultiValueConverter : IMultiValueConverter
{
    #region Implementation of IMultiValueConverter

    /// <inheritdoc />
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        SolidColorBrush mainred = new SolidColorBrush(System.Windows.Media.Color.FromRgb(223,70,75));
        var columnHeader = values[0] as string;
        var dataItem = values[1] as Task;
        if( columnHeader.Equals("Status", StringComparison.OrdinalIgnoreCase)
               && dataItem.Status.Equals("Success!", StringComparison.OrdinalIgnoreCase))
        {
            return mainred;
        }
        if( columnHeader.Equals("#", StringComparison.OrdinalIgnoreCase))
        {
            return mainred;
        }
        if( columnHeader.Equals("Proxy"))
        {
            if(dataItem.Proxy)
            {
                return Brushes.LightGreen;
            }
            return mainred;
        }


        return Brushes.White;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => throw new NotSupportedException();

    #endregion
}

像这样链接到我的 DataGrid:

<DataGrid.CellStyle>
                             <Style TargetType="{x:Type DataGridCell}">
                                    ...
                                    <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>

我有一个 DataGridTemplateColumn,它显示您在 DataGrid 中的项目数,如下所示:

<DataGridTemplateColumn Width="*" Header="#" MaxWidth="60">
                                    <DataGridTemplateColumn.HeaderStyle>
                                        <Style BasedOn="{StaticResource MaterialDesignDataGridColumnHeader}" TargetType="{x:Type DataGridColumnHeader}">
                                            <Setter Property="HorizontalAlignment" Value="Left" />
                                        </Style>
                                    </DataGridTemplateColumn.HeaderStyle>
                                    <DataGridTemplateColumn.CellStyle>
                                        <Style TargetType="{x:Type DataGridCell}">
                                            <Setter Property="Padding" Value="20"/>
                                            <!--<Setter Property="Height" Value="57"/>-->
                                            <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="#262626" CornerRadius="5,5,5,5" BorderThickness="2" BorderBrush="{Binding StatusColour}">
                                                <Border.Style>
                                                    <Style TargetType="Border">

                                                    </Style>
                                                </Border.Style>
                                                <TextBlock Text="{Binding NumberOTasks}" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" Foreground="#E43D47"/>
                                            </Border>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>

如果我将 IMultiValueConverter 放在 DataGridTemplateColumn.CellStyle 中,它会工作并为边框添加颜色,但它不受边框角半径的影响,但如果我把它放在边框样式,它不起作用并表示列 header is = null。我怎样才能更改边框画笔并且它会受到边框角半径的影响?

您可以为每列设置单元格样式或使用基于值的全局单元格样式:

<DataGrid xmlns:system="clr-namespace:System;assembly=mscorlib">
  <DataGrid.Resources>
   <Style TargetType="DataGridCell">
    <Style.Triggers>
     <DataTrigger Binding="{Binding}"
                  Value="Proxy">
      <Setter Property="Foreground"
              Value="LightGreen" />
      <Setter Property="BorderBrush"
              Value="LightGreen" />
     </DataTrigger>
     <DataTrigger Binding="{Binding}"
                  Value="#">
      <Setter Property="Foreground"
              Value="Gray" />
      <Setter Property="BorderBrush"
              Value="Red" />
     </DataTrigger>
    </Style.Triggers>
   </Style>
  </DataGrid.Resources>
  <DataGrid.Columns>
   <DataGridTextColumn Binding="{Binding}"
                       Header="#" />
   <DataGridTextColumn Binding="{Binding}"
                       Header="Proxy" />
   <DataGridTextColumn  Binding="{Binding}"
                        Header="Status">
    <DataGridTextColumn.CellStyle>
     <Style TargetType="DataGridCell">
      <Setter  Property="Foreground"
               Value="Blue" />
      <Setter  Property="BorderBrush"
               Value="Blue" />
     </Style>
    </DataGridTextColumn.CellStyle>
   </DataGridTextColumn>
  </DataGrid.Columns>
  <system:String>Proxy</system:String>
  <system:String>#</system:String>
  <system:String>Success</system:String>
 </DataGrid> 

结果应如下所示:

要更改内部网格边框,您需要覆盖 DataGridCell 的默认模板并从那里修改边框属性。请记住,ContentControl 有两个模板:ControlTemplate 用于 ContentControl 本身(例如 ListBoxItemDataGridCell)和 DataTemplate对于 ContentControl.

的内容
<DataGridTemplateColumn.CellStyle>
  <Style TargetType="DataGridCell">
    <Setter Property="Background" Value="#262626">
    <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>

    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="DataGridCell">
          <Border BorderThickness="{TemplateBinding BorderThickness}"
                  BorderBrush="{TemplateBinding BorderBrush}" 
                  Background="{TemplateBinding Background}" 
                  CornerRadius="5">
            <ContentPresenter />
          </Border>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</DataGridTemplateColumn.CellStyle>