简体   繁体   中英

DatagridTextColumn center text

I have a datagrid. Some of the cells are different colors depending on their value. This works fine.

The issue is that when I center the text the cell loses its color, the font is coloured but I want the cell to be filled as well.

Below is my code which fills the cell and changes the font color the only thing missing is the text is not centered.

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

When I add the line above though like I say the cell is no longer colored although the font is, why?

<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>

EDIT: As @ChrisW. mentions in the comment section, TextAlignment=Center is the real answer and you don't need a wrapper around Textblock. Since this is the only answer right now, I'm leaving an edit here instead of completely retracting it. The below code can be used to handle other situation where you don't have the option of TextAlignment or HorizontalContentAlignment .


Actually the code is working exactly it should. When you say HorizontalAlignment=Center , the TextBlock shrinks to its content length and then centers itself in the container. In order to achieve what you want, you need to add a wrapper control around the centered text block that then handles the full cell width fill.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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