Silverlight DataGrid DataGridTemplateColumn 单元格对齐问题,左对齐,右对齐都可以用。
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
首先,添加在Resources中添加Style,其TargetType值为DataGridCell;
<UserControl.Resources> <Style x:Name="DataGridElementStyle" TargetType="sdk:DataGridCell"> <Setter Property="HorizontalAlignment" Value="Right"></Setter> <Setter Property="Padding" Value="0,0,20,0"></Setter> </Style> </UserControl.Resources>
其次,在DataGirdTemplateColumn添加属性CellStyle,将其值与Resources中的Style绑定。
<sdk:DataGrid x:Name="HistoricContrastCurrentResultsDataGrid" ItemsSource="{Binding ContrastData.HistoricContrastCurrentResults}" SelectedItem="{Binding ContrastData.SelectedHistoricContrastCurrentResult, Mode=TwoWay}" Height="180" RowHeight="22" Grid.Row="1" Grid.Column="3" AutoGenerateColumns="False" FontSize="12" CanUserSortColumns="True" CanUserResizeColumns="True" IsReadOnly="True" Margin="5,0,0,0"> <sdk:DataGrid.Columns> <sdk:DataGridTemplateColumn Header="重叠部分面积(平方米)" CanUserSort="True" SortMemberPath="OverlayArea" CellStyle="{StaticResource DataGridElementStyle}"> <sdk:DataGridTemplateColumn.CellTemplate> <DataTemplate> <HyperlinkButton Command="{Binding ZoomToOverlayGraphicsCommand}"> <ToolTipService.ToolTip> <ToolTip Style="{StaticResource OverlayAreaToolTipStyle}"></ToolTip> </ToolTipService.ToolTip> <TextBlock Text="{Binding OverlayArea,StringFormat=n2}" FontSize="14"/> </HyperlinkButton> </DataTemplate> </sdk:DataGridTemplateColumn.CellTemplate> </sdk:DataGridTemplateColumn> <sdk:DataGridTemplateColumn Header="非重叠部分面积(平方米)" CanUserSort="True" SortMemberPath="DifferentArea" CellStyle="{StaticResource DataGridElementStyle}"> <sdk:DataGridTemplateColumn.CellTemplate> <DataTemplate> <HyperlinkButton Command="{Binding ZoomToDifferentGraphicsCommand}"> <ToolTipService.ToolTip> <ToolTip Style="{StaticResource DifferentAreaToolTipStyle}"></ToolTip> </ToolTipService.ToolTip> <TextBlock Text="{Binding DifferentArea,StringFormat=n2}" FontSize="14"/> </HyperlinkButton> </DataTemplate> </sdk:DataGridTemplateColumn.CellTemplate> </sdk:DataGridTemplateColumn> </sdk:DataGrid.Columns> </sdk:DataGrid>
效果如下图: