Post view page

Post #18

WPF Datagrid Alignment
2012-01-06 07:43:14

I use the following for alignment in the WPF DataGrid Columns.

First set a style in the resources for either the window or the user control.

Note you can add other properties here if needed.

<Window.Resources>
    <Style x:Key="StyleAlignRight"
           TargetType="{x:Type TextBlock}">
        <Setter Property="TextAlignment"
                Value="Right" />
    </Style>
    <Style x:Key="StyleAlignCenter"
           TargetType="{x:Type TextBlock}">
        <Setter Property="TextAlignment"
                Value="Center" />
    </Style>
</Window.Resources>

Then in the datagrid column set the element style

<DataGridTextColumn Header="Calls"
                    ElementStyle="{StaticResource StyleAlignRight}"
                    Binding="{Binding Path=DailyCalls, StringFormat=N0, Mode=TwoWay}"
                    MinWidth="50"
                    CanUserSort="True"  />

 

Test right content

first test

Test right content

second test

Test right content

third test

this is a test