Column Attribute in Silverlight DataGrid

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

The approach given for DataGrids WPF in this other question does not seem to work in Silverlight.

+2
silverlight datagrid
Oct. 27 '11 at 13:03
source share
3 answers

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 the column as follows:

 <sdk:DataGridTextColumn Header="Header" Binding="{Binding Binding}" CellStyle="{StaticResource RightCellStyle}" /> 
+7
Oct. 27 '11 at 13:08
source share

I think you want a HorizontalContentAlignment. If it is in style:

 <Setter Property="HorizontalContentAlignment" Value="Center" /> 
0
Oct 27 '11 at 13:08
source share

You just need to properly 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> 
0
Oct 27 '11 at 13:19
source share



All Articles