如何从后面的代码将文本对齐方式应用于LoadingRow事件上的数据网格行

问题描述 投票:1回答:1

我有一个在运行时填充的数据网格。我想强调一些数据。为此,我使用了LoadingRow事件。

[enter image description here enter image description here

下面的代码有效:

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;

谢谢帕特里克

c# wpf datagrid row text-alignment
1个回答
0
投票

您应将列的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>
© www.soinside.com 2019 - 2024. All rights reserved.