繁体   English   中英

DatagridTextColumn中心文本

[英]DatagridTextColumn center text

我有一个数据网格。 某些单元格的颜色取决于其值。 这很好。

问题是,当我居中放置文本时,单元格会失去颜色,字体是彩色的,但我也希望单元格也能填充。

下面是我的代码,它填充单元格并更改字体颜色,唯一缺少的是文本未居中。

<Setter Property="HorizontalAlignment" Value="Center"/>

当我像上面说的那样在上面添加行时,尽管字体是白色,但单元格不再是彩色的,为什么?

<DataGridTextColumn Header="BTBL" IsReadOnly="True" MinWidth="75" Binding="{Binding BTBL.DisplayString}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="FontWeight" Value="Bold"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding BTBL.ColourCode}" Value="GREEN">
                    <Setter Property="Background" Value="LightGreen"/>
                    <Setter Property="Foreground" Value="DarkGreen"/>                                                                                     
                </DataTrigger>
                <DataTrigger Binding="{Binding BTBL.ColourCode}" Value="YELLOW">
                    <Setter Property="Background" Value="LightYellow"/>
                    <Setter Property="Foreground" Value="DarkKhaki"/>                                        
                </DataTrigger>
                <DataTrigger Binding="{Binding BTBL.ColourCode}" Value="RED">
                    <Setter Property="Background" Value="LightCoral"/>
                    <Setter Property="Foreground" Value="Red"/>                                       
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

编辑:作为@ChrisW。 在注释部分提到, TextAlignment=Center是真正的答案,您不需要在Textblock周围包装。 由于这是目前唯一的答案,因此我将在此处保留修改,而不是完全撤消它。 以下代码可用于处理其他无法选择TextAlignmentHorizontalContentAlignment


实际上,代码完全可以正常工作。 当您说HorizontalAlignment=CenterTextBlock缩小到其内容长度,然后在容器中居中。 为了实现所需的功能,您需要在居中的文本块周围添加包装器控件,然后处理整个单元格宽度填充。

<DataGridTemplateColumn Header="BTBL" IsReadOnly="True" MinWidth="75">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Border BorderThickness="0">
                <TextBlock Text="{Binding BTBL.DisplayString}">
                    <TextBlock.Style>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="HorizontalAlignment" Value="Center" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding BTBL.ColourCode}" Value="GREEN">
                                    <Setter Property="Foreground" Value="DarkGreen"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding BTBL.ColourCode}" Value="YELLOW">
                                    <Setter Property="Foreground" Value="DarkKhaki"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding BTBL.ColourCode}" Value="RED">
                                    <Setter Property="Foreground" Value="Red"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
                <Border.Style>
                    <Style TargetType="{x:Type Border}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding BTBL.ColourCode}" Value="GREEN">
                                <Setter Property="Background" Value="LightGreen"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding BTBL.ColourCode}" Value="YELLOW">
                                <Setter Property="Background" Value="LightYellow"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding BTBL.ColourCode}" Value="RED">
                                <Setter Property="Background" Value="LightCoral"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
            </Border>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM