Silverlight DataGrid中的列对齐

时间:2011-10-27 13:03:24

标签: .net silverlight datagrid

如何在Silverlight DataGrid中设置单元格内容的对齐方式?

this other question为WPF DataGrids提供的方法在Silverlight中似乎不起作用。

3 个答案:

答案 0 :(得分:7)

在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;
});

或者在XAML ......

添加XMLNS:

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

添加此资源:

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

设置如下列:

<sdk:DataGridTextColumn Header="Header" Binding="{Binding Binding}"
    CellStyle="{StaticResource RightCellStyle}" /> 

答案 1 :(得分:0)

我认为你想要Horizo​​ntalContentAlignment。 如果它的风格:

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

答案 2 :(得分:0)

只需右对齐DataGridCell,就像这样,

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