在设计模式和运行时看起来不同

时间:2010-09-02 11:08:12

标签: wpf

我的WPF问题很小。 这是我的Style,我为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>

    

这是Grid中的DataGrid视图控件:

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

问题是我可以在VS设计器或Blend中看到修改过的标题,但是当我运行我的应用程序标题样式时是默认的。有人可以帮忙吗?提前谢谢。

修改

刚发现有趣的事情。当我把Style放进去

  

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 个答案:

答案 0 :(得分:1)

看起来DynamicResource在运行时没有正确解析。尝试将DynamicResource切换为StaticResource,如下所示:

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

有关DynamicResource和StaticResource之间差异的更多详细信息,请参阅此其他StackOverflow问题:What's the difference between StaticResource and DynamicResource in WPF?