首页 > 解决方案 > 如何使用 DataTrigger 将样式应用于 DataGridRow

问题描述

我正在尝试将一些条件样式应用于 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. 当转换器返回 true 时应用红色字体(无效记录)

  2. Apply a red font also when the row is selected and the convertor returns true, at the moment it changes to a default of white when selected, I want it to stay red.

  3. 当转换器恢复为真时显示一个工具提示,此时它只是弹出一个框,上面写着“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();
    }
  }

标签: wpfxamldatatriggerdatagridrow

解决方案


如果要更改Foreground,则应指定 aCellStyle而不是 a RowStyle

您也可以直接将ToolTip属性设置为 a string。只要确保您没有将ToolTip属性设置在Style.

尝试这个:

<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。我只添加了您的触发器。您当然可以根据您的确切要求进行编辑。


推荐阅读