2

How can you set the alignment of cell content in a Silverlight DataGrid?

The approach given for WPF DataGrids at this other question doesn't seem to work in Silverlight.

Community
  • 1
  • 1
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742

3 Answers3

7

In C#:

var rightCellStyle = new Style(typeof(DataGridCell));
rightCellStyle.Setters.Add(new Setter(
    Control.HorizontalContentAlignmentProperty,
    HorizontalAlignment.Right));
dataGrid.Columns.Add(new DataGridTextColumn {
    Binding = /* binding */,
    Header = /* header */,
    CellStyle = rightCellStyle;
});

Or in XAML...

Add XMLNS:

xmlns:sdk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"

Add this resource:

<Style x:Key="RightCellStyle" TargetType="sdk:DataGridCell">
  <Setter Property="HorizontalContentAlignment" Value="Right" />
</Style>

Set on a column like this:

<sdk:DataGridTextColumn Header="Header" Binding="{Binding Binding}"
    CellStyle="{StaticResource RightCellStyle}" /> 
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
1

Just need to right align the DataGridCell, like this,

    <sdk:DataGrid Margin="104,82,139,71" AutoGenerateColumns="False" ItemsSource="{Binding Collection}">
        <sdk:DataGrid.CellStyle>
            <Style TargetType="sdk:DataGridCell">
                <Setter Property="HorizontalContentAlignment" Value="Right"/>
            </Style>
        </sdk:DataGrid.CellStyle>
Justin XL
  • 38,763
  • 7
  • 88
  • 133
0

I think you want the HorizontalContentAlignment. If its in a style:

<Setter Property="HorizontalContentAlignment" Value="Center" /> 
Jay
  • 5,897
  • 1
  • 25
  • 28