我有一个在运行时填充的数据网格。我想强调一些数据。为此,我使用了LoadingRow事件。
下面的代码有效:
dtg_ExecutionTimes_PpDescriptions.LoadingRow += (sender, args) =>
{
int rowNum = args.Row.GetIndex();
DataGridRow row = (DataGridRow)dtg_ExecutionTimes_PpDescriptions.ItemContainerGenerator.ContainerFromIndex(rowNum);
switch (m_ListCells[rowNum].Arm)
{
case CfgPartPrograms.eArm.ARM1: break;
case CfgPartPrograms.eArm.ARM2:
row.Foreground = Brushes.GreenYellow; <-------THAT WORKS :-)
row.HorizontalAlignment = row.HorizontalContentAlignment = HorizontalAlignment.Right;<-----THAT DOESN'T WORK :-(
break;
case CfgPartPrograms.eArm.ARM12: row.Foreground = Brushes.LightSalmon; break;
default:
break;
}
};
我也尝试过样式,但是由于某些原因我没有效果
var MyStyle = new Style(typeof(DataGridRow)) { Setters = { new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Right) } };
row.Style = MyStyle;
谢谢帕特里克
您应将列的ElementStyle
设置为右对齐单元格值。
如果只想将Foreground
的值右对齐Brushes.GreenYellow
,则可以在DataTrigger
上添加Style
:
<DataGridTextColumn Binding="..." Header="Part program decscription" Width="200">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding Foreground,
RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="GreenYellow">
<Setter Property="HorizontalAlignment" Value="Right" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>