silverlight DataGrid样式DataGrid表头一直无法居中显示,不知如何调整,望各位大牛指点。我用的样式是Toolki
silverlight DataGrid样式
DataGrid表头一直无法居中显示,不知如何调整,望各位大牛指点。
我用的样式是Toolkit中的样式文件其中用到控件模板!
C# code<Style TargetType="dataPrimitives:DataGridColumnHeader"> <Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="dataPrimitives:DataGridColumnHeader"> <Grid x:Name="Root" Width="Auto"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <vsm:VisualStateManager.VisualStateGroups> ........ </vsm:VisualStateManager.VisualStateGroups> [color=#800000]<Rectangle x:Name="BackgroundGradient" Grid.ColumnSpan="3" Stretch="Fill" Fill="{StaticResource GridHeaderBackground}" />[/color] <ContentPresenter Margin="{TemplateBinding Padding}" HorizontalAlignment="Center" VerticalAlignment="Center" Content="{TemplateBinding Content}" Cursor="{TemplateBinding Cursor}"/> ....... </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
红色字体标注的是表头的背景色,但是不管我怎么弄,表头就是不居中显示!
[解决办法]你尝试对DataGrid的表头进行设置呢。不要单独设置
[解决办法]使用Blend编辑Datagrid Header模板,设置文本居中。
请参考,自定义Datagrid Header:
http://silverlightchina.net/html/tips/2010/0102/536.html
http://silverlightchina.net/plus/search.php?kwtype=0&keyword=datagrid+template&searchtype=titlekeyword
[解决办法]sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="名称" HeaderStyle="{StaticResource DataGridHeaderStyle}" CellStyle="{StaticResource DataGridCellStyle}"> </sdk:DataGridTextColumn>
</sdk:DataGrid.Columns>
加上这个了吗?
[解决办法]解决方案:
1.把下面的资源代码添加到你的 APP.XAML文件<Application.Resources>…………</Application.Resources>中;
2.应用相应的命名空间
xmlns:prim="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
xmlns:local="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows"
xmlns:localprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
(以上命名空间有的可能用不到)
3.在你的DataGrid对象中调用静态资源, HeaderStyle="{StaticResource DataGridHeaderStyle}" ;
4.下面代码是写的列头的,也可以添加单元格的样式。以上可以解决你的问题。
------解决方案--------------------
5.代码:
<Style TargetType="prim:DataGridColumnHeader" x:Key="ColHeaderStyle">
<!--<Setter Property="BorderThickness" Value="0.5,0.5,0.5,0.5"/>-->
<!--<Setter Property="HorizontalConAlignment" Value="Center"/>-->
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Foreground" Value="#9b010d"/>
<!--<Setter Property="VerticalAlignment" Value="Top"/>-->
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="FontSize" Value="24"/>
<!--<Setter Property="FontWeight" Value="Bold"/>-->
<!--<Setter Property="IsTabStop" Value="False" /-->
<!--<Setter Property="SeparatorBrush" Value="#FFDFE3E6" />-->
<!--<Setter Property="Padding" Value="4,4,5,4" />-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="prim:DataGridColumnHeader">
<Grid Name="Root">
<ContentPresenter
Content="{TemplateBinding Content}"
Cursor="{TemplateBinding Cursor}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
<!--<Rectangle Name="VerticalSeparator" Grid.RowSpan="2" Grid.Column="2" Width="1" VerticalAlignment="Stretch" Fill="{TemplateBinding SeparatorBrush}" Visibility="{TemplateBinding SeparatorVisibility}" />-->
<!--<Path Grid.RowSpan="2" Name="SortIcon" RenderTransformOrigin=".5,.5" HorizontalAlignment="Center" VerticalAlignment="Center" Opacity="0" Grid.Column="1" Stretch="Uniform" Width="8" Data="F1 M -5.215,6.099L 5.215,6.099L 0,0L -5.215,6.099 Z ">
<Path.RenderTransform>
<TransformGroup>
<ScaleTransform x:Name="SortIconTransform" ScaleX=".9" ScaleY=".9" />
</TransformGroup>
</Path.RenderTransform>
</Path>-->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="HeaderStyle" TargetType="prim:DataGridColumnHeader">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="prim:DataGridColumnHeader">
<Grid Name="Root">
<ContentPresenter
Content="{TemplateBinding Content}"
Cursor="{TemplateBinding Cursor}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<!--<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="Black" Offset="0.5"/>
<GradientStop Color="Black" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>-->
<Setter Property="Foreground" Value="#9b010d"/>
<Setter Property="BorderThickness" Value="1,1,1,1"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>