开发者

Different look in design-mode and at runtime

I have small problem with WPF. Here is my Style where I created a template for DataGrid Column Header.

<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>
   <Mi开发者_Python百科crosoft_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>

Here's my DataGrid view control in Grid:

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

The problem is I can see modified header in VS designer, or in Blend, but when I run my application header style is default. Can anyone help? Thanks in advance.

EDIT

Just found interesting thing. When I put Style in

DataGridTemplateColumn.HeaderStyle

it's working

       <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>

Why does it make a difference?


It looks like the DynamicResource is not resolving correctly at runtime. Try switching your DynamicResource to a StaticResource like so:

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

For more details on the differences between DynamicResource and StaticResource, see this other StackOverflow question: What's the difference between StaticResource and DynamicResource in WPF?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜