2010-09-02 54 views
0

我對WPF有一些小問題。 這是我創建DataGrid列標題模板的樣式。設計模式和運行時的不同外觀

<Window.Resources> 
    <Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}"> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="Template"> 
<Setter.Value> 
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}"> 
    <Grid> 
    <Microsoft_Windows_Themes:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" IsClickable="{TemplateBinding CanUserSort}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" Padding="{TemplateBinding Padding}" SortDirection="{TemplateBinding SortDirection}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}"> 
    <Microsoft_Windows_Themes:DataGridHeaderBorder.Background> 
    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
     <GradientStop Color="#FF7EE4FF" Offset="0"/> 
     <GradientStop Color="#FF66D6F3" Offset="1"/> 
     <GradientStop Color="#FF0097BE" Offset="0.5"/> 
    </LinearGradientBrush> 
    </Microsoft_Windows_Themes:DataGridHeaderBorder.Background> 
    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
    </Microsoft_Windows_Themes:DataGridHeaderBorder> 
    </Grid> 
</ControlTemplate> 
</Setter.Value> 

這裏的網格我的DataGrid控件:

<DataGrid Margin="46.667,41.333,102,87"> 
    <DataGrid.Columns> 
     <DataGridTemplateColumn Header="Status" Width="*" HeaderStyle=" {DynamicResource  DataGridColumnHeaderStyle1}"/> 
    </DataGrid.Columns> 
</DataGrid> 

的問題是,我可以看到修改後的頭在VS設計師,或在Blend,但是當我跑我的應用程序標題樣式是默認的。誰能幫忙?提前致謝。

編輯

剛剛找到有趣的事情。當我把風格

DataGridTemplateColumn.HeaderStyle

它的工作

 <DataGrid Margin="46.667,41.333,102,87"> 
     <DataGrid.Columns> 
      <DataGridTemplateColumn Header="Status" Width="*"> 
       <DataGridTemplateColumn.HeaderStyle> 
        <Style TargetType="{x:Type DataGridColumnHeader}"> 
         <Setter Property="VerticalContentAlignment" Value="Center"/> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate TargetType="{x:Type DataGridColumnHeader}"> 
            <Grid> 
             <Microsoft_Windows_Themes:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" IsClickable="{TemplateBinding CanUserSort}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" Padding="{TemplateBinding Padding}" SortDirection="{TemplateBinding SortDirection}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}"> 
              <Microsoft_Windows_Themes:DataGridHeaderBorder.Background> 
               <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
                <GradientStop Color="#FF7EE4FF" Offset="0"/> 
                <GradientStop Color="#FF66D6F3" Offset="1"/> 
                <GradientStop Color="#FF0097BE" Offset="0.5"/> 
               </LinearGradientBrush> 
              </Microsoft_Windows_Themes:DataGridHeaderBorder.Background> 
              <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
             </Microsoft_Windows_Themes:DataGridHeaderBorder> 
            </Grid> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
        </Style> 
       </DataGridTemplateColumn.HeaderStyle> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns> 
    </DataGrid> 

爲什麼會有所作爲?

回答

1

它看起來像DynamicResource在運行時不能正確解析。試試你的DynamicResource切換到靜態資源,像這樣:

<DataGridTemplateColumn Header="Status" Width="*" 
    HeaderStyle="{StaticResource DataGridColumnHeaderStyle1}"/> 

有關DynamicResource和StaticResource的之間的差異的詳細信息,請參閱本其他StackOverflow的問題:What's the difference between StaticResource and DynamicResource in WPF?

+0

感謝的人。現在正在工作 – pavel 2010-09-02 12:05:17

+0

不客氣。 :) – 2010-09-02 12:11:51