2011-10-27 55 views

回答

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}" /> 
0

我想你想要Horizo​​ntalContentAlignment。 如果它的風格:

<Setter Property="HorizontalContentAlignment" Value="Center" /> 
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>