wpf - 如何使用 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 来做三件事:
当转换器返回 true 时应用红色字体(无效记录)
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.
当转换器恢复为真时显示一个工具提示,此时它只是弹出一个框,上面写着“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
,则应指定 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
。我只添加了您的触发器。您当然可以根据您的确切要求进行编辑。
推荐阅读
- c++ - 除了 memcpy,bit_cast 还会有哪些额外的 UB(如果有的话)?
- python - Python SUBPROCESS.RUN 说文件不在位,虽然它是
- perl - 使用多个条件对嵌套哈希进行排序
- javascript - 如何循环嵌套对象 - javascript
- css - 如何在 CSS 中使用不同的形状形成一个对象?
- python - 发送自定义 tcp 数据包时避免 RST 数据包
- c# - C#8:切换 ref 表达式
- reactjs - reactjs用我的套接字日期填充组件
- node.js - 关于 reactJS、nodeJS 和 NPM 的一般问题
- java - Java .. 给定公共类并且没有 Main 方法作为组项目的一部分 .. 我如何编译?