如何使用 DataTrigger 将样式应用于 DataGridRow

How to apply styles to DataGridRow with DataTrigger

我正在尝试将一些条件样式应用于 DataGridRow,这是我目前所拥有的:

  <DataGrid.ItemContainerStyle>
    <Style TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource {x:Type DataGridRow}}">
      <Style.Triggers>
        <DataTrigger Value="True">
          <DataTrigger.Binding>
            <MultiBinding Converter="{StaticResource SBCInvalidHighlightConverter}">
              <Binding Path="." />
              <Binding Path="DataContext.SelectedCaseType" RelativeSource="{RelativeSource AncestorType=DataGrid}" />
              <Binding Path="IsSelected" RelativeSource="{RelativeSource AncestorType=DataGridRow}"/>
            </MultiBinding>
          </DataTrigger.Binding>
          <Setter Property="Foreground" Value="Red"/>
          <Setter Property="ToolTip">
            <Setter.Value>
              <TextBlock Text="This criteria will not be applied"/>
            </Setter.Value>
          </Setter>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </DataGrid.ItemContainerStyle>

基本上我正在尝试 运行 这个转换器 SBCInvalidHighlightConverter 做三件事:

  1. 当转换器 returns 为真(无效记录)时应用红色字体

  2. 当行被选中并且转换器 returns 为真时,也应用红色字体,目前它在被选中时变为默认的白色,我希望它保持红色。

  3. 当转换器返回true时显示工具提示,此时它只是弹出一个框说"System.Windows.Controls.TextBlock"

我让第一个开始工作,但第二个和第三个没有。

图像显示选择和光标悬停与弹出文本:

所以问题是:如何让第 2 项和第 3 项发挥作用?

这里是转换器,不确定是否需要:

  public class SBCInvalidHighlightConverter : IMultiValueConverter
  {
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      bool result = false;

      if (values == null || values.Length < 2)
        return result;

      CTS_EF_DAL.RBCRuleValueForDisplay rowData = (CTS_EF_DAL.RBCRuleValueForDisplay)values[0];

      SBC.SubstanceTypeCode criteriaType = (SBC.SubstanceTypeCode)rowData.Rule_Typ_Cd;
      SBC.CaseType caseType = (SBC.CaseType)((int)values[1]);

      if (caseType != SBC.CaseType.All)
      {
        var caseTypeAttribute = criteriaType.GetAttribute<SBC.CaseTypeAttribute>();
        if (caseTypeAttribute != null && caseTypeAttribute.CaseType != SBC.CaseType.All)
        {
          if (caseTypeAttribute.CaseType != caseType)
          {
            return true; //It is invalid
          }
        }
      }

      return result;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
      throw new NotImplementedException();
    }
  }

如果要更改 Foreground,您应该指定 CellStyle 而不是 RowStyle

您也可以直接将 ToolTip 属性 设置为 string。只需确保您没有在 Style 之外的其他地方设置 ToolTip 属性。

试试这个:

<DataGrid.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
            </Trigger>
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="true"/>
                    <Condition Property="Selector.IsSelectionActive" Value="false"/>
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/>
            </MultiTrigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
            </Trigger>
            <DataTrigger Value="True">
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource SBCInvalidHighlightConverter}">
                        <Binding Path="." />
                        <Binding Path="DataContext.SelectedCaseType" RelativeSource="{RelativeSource AncestorType=DataGrid}" />
                        <Binding Path="IsSelected" RelativeSource="{RelativeSource AncestorType=DataGridRow}"/>
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Foreground" Value="Red"/>
                <Setter Property="ToolTip" Value="This criteria will not be applied" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.CellStyle>

上面的 Style 是基于 Windows 10 上 DataGridCell 的默认样式。我只添加了您的触发器。您当然可以根据您的具体要求进行编辑。