【问题标题】:How to apply styles to DataGridRow with DataTrigger如何使用 DataTrigger 将样式应用于 DataGridRow
【发布时间】:2020-07-26 19:30:13
【问题描述】:

我正在尝试将一些条件样式应用于 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. 在选择行并且转换器返回 true 时也应用红色字体,此时它在选择时更改为默认的白色,我希望它保持红色。

  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();
    }
  }

【问题讨论】:

    标签: wpf xaml datatrigger datagridrow


    【解决方案1】:

    如果您想更改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 的默认样式。我只添加了您的触发器。您当然可以根据您的确切要求进行编辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-15
      • 2011-07-21
      • 2017-07-06
      • 2019-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      相关资源
      最近更新 更多