我在Silverlight 4页面上有四个数据网格。我正在尝试为每个网格设置不同的列标题样式。我发现这个XAML在我将其嵌入<sdk:DataGrid.ColumnHeaderStyle>
标记内的每个DataGrid时都有效:
<Style TargetType="primitives:DataGridColumnHeader" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="primitives:DataGridColumnHeader">
<Grid Name="Root">
<vsm:VisualStateManager.VisualStateGroups>
<vsm:VisualStateGroup x:Name="SortStates" >
<vsm:VisualStateGroup.Transitions>
<vsm:VisualTransition GeneratedDuration="00:00:0.1" />
</vsm:VisualStateGroup.Transitions>
<vsm:VisualState x:Name="Unsorted" />
<vsm:VisualState x:Name="SortAscending">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="SortIcon" Storyboard.TargetProperty="Opacity" Duration="0" To="1.0" />
</Storyboard>
</vsm:VisualState>
<vsm:VisualState x:Name="SortDescending">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="SortIcon" Storyboard.TargetProperty="Opacity" Duration="0" To="1.0" />
<DoubleAnimation Storyboard.TargetName="SortIconTransform" Storyboard.TargetProperty="ScaleY" Duration="0" To="-.9" />
</Storyboard>
</vsm:VisualState>
</vsm:VisualStateGroup>
</vsm:VisualStateManager.VisualStateGroups>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Rectangle x:Name="BackgroundRectangle" Stretch="Fill" Grid.ColumnSpan="2" Grid.RowSpan="2">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="Transparent" Offset="0" />
<GradientStop Color="LavenderBlush" Offset="1" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter Grid.RowSpan="2" 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="Left" 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.Fill>
<SolidColorBrush Color="#FF444444" />
</Path.Fill>
<Path.RenderTransform>
<TransformGroup>
<ScaleTransform x:Name="SortIconTransform" ScaleX=".9" ScaleY=".9" />
</TransformGroup>
</Path.RenderTransform>
</Path>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
当我将它放在一个单独的资源字典中并删除上述标记时,它也适用于所有四个标题。但是当我尝试在资源字典中创建它的特定版本但将第一行更改为:
<Style x:Key="ADGridColumnHeader" TargetType="primitives:DataGridColumnHeader" >
并将其添加到DataGrid:
ColumnHeaderStyle="PhoneMasterGridColumnHeader"
我收到错误:XamlParseException发生:无法从文本'PhoneMasterGridColumnHeader'创建'System.Windows.Style'。我无法弄清楚什么是错的。我仍然在努力掌握Styles和Resource Dictionaries。有什么想法吗?
答案 0 :(得分:7)
试试这样: -
ColumnHeaderStyle="{StaticResource PhoneMasterGridColumnHeader}"
ColumnHeaderStyle属性期望接收类型为Style
的对象。现在您可以像这样定义该值: -
<DataGrid.ColumnHeaderStyle>
<Style TargetType="primitives:DataGridColumnHeade">
<!-- You setters here -->
</Style>
</DataGrid>
或者您可能出于某种奇怪的原因,在ViewModel上将该样式公开为名为HeaderStyle
的属性,该属性是网格的当前DataContext
: -
<DataGrid ColumnHeaderStyle="{Binding HeaderStyle}" >
关键是在Xaml中可以通过几种不同的方式获取属性值。引用资源只是其中之一。因此,简单地说明ColumnHeaderStyle="MyStyle"
至少是不明确的,所以我们需要说明我们想要通过静态资源获取值的状态。