6

我在 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”。我不知道出了什么问题。我仍在努力掌握样式和资源字典。任何的想法?

4

1 回答 1

7

像这样尝试:-

 ColumnHeaderStyle="{StaticResource PhoneMasterGridColumnHeader}"

ColumnHeaderStyle 属性期望接收类型为 的对象Style。现在您可以像这样定义该值:-

 <DataGrid.ColumnHeaderStyle>
    <Style TargetType="primitives:DataGridColumnHeade">
      <!--  You setters here -->
    </Style>
 </DataGrid>

或者您可能出于某种奇怪的原因将样式公开为HeaderStyleViewModel 上的一个属性,该属性是DataContext网格的当前:-

 <DataGrid ColumnHeaderStyle="{Binding HeaderStyle}" >

关键是在 Xaml 中可以通过多种不同的方式获取属性值。引用资源只是其中之一。因此,简单的陈述ColumnHeaderStyle="MyStyle"至少是模棱两可的,所以我们需要明确表示我们希望通过静态资源获取价值。

于 2010-08-31T19:37:40.120 回答