Search code examples
datagriduwp-xamlwindows-community-toolkit

UWP Community Toolkit DataGrid control : how to align cell content to right


I have a numeric column in a DataGrid I want its content be right-aligned. I have read the docs, mostly Customizable templates and styling for cells, rows and headers where I ended up with this code to accomplish right-align cell content:

<controls:DataGrid.CellStyle>
    <Style TargetType="controls:DataGridCell">
        <Style.Setters>
            <Setter Property="HorizontalAlignment" Value="Right"/>
        </Style.Setters>
    </Style>
</controls:DataGrid.CellStyle>

As is to be expected, this code really does what I want, but with all the cells. I want only the numeric column to have this property, and only it, but I have been unable to accomplish this.

What am I doing wrong?


Solution

  • I want only the numeric column to have this property, and only it

    For your requirement, you could give cell style to specific DataGridTextColumn property like the follow.

    <controls:DataGridTextColumn  >
        <controls:DataGridTextColumn.CellStyle>
            <Style TargetType="controls:DataGridCell">
                <Style.Setters>
                    <Setter Property="HorizontalAlignment" Value="Right"/>
                </Style.Setters>
            </Style>
        </controls:DataGridTextColumn.CellStyle>
    </controls:DataGridTextColumn>
    

    The style will work for specific column.