WPF,样式未应用 [英] Wpf, style is not being applied

查看:57
本文介绍了WPF,样式未应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用弹出窗口编写了一个用户控件,该用户的内容是在控件外部设置的.该控件的ControlTemplate如下所示:

I've written a user control with popup, who's content is being set outside the control. The ControlTemplate of that control looks like the following:

<ControlTemplate TargetType="local:InfoIcon">
    <Grid>
        <ToggleButton x:Name="HelpButton" Style="{StaticResource HelpButton}" />
        <Popup PlacementTarget="{Binding ElementName=HelpButton}" Placement="Bottom"
                IsOpen="{Binding ElementName=HelpButton, Path=IsChecked, Mode=TwoWay}" StaysOpen="False">
            <Border BorderBrush="#767676" BorderThickness="1" 
                    Background="#f1f2f7">
                <Border.Resources>

                    <!-- Important -->
                    <Style TargetType="Label">
                        <Setter Property="Foreground" Value="#575757" />
                    </Style>
                    <Style TargetType="TextBlock">
                        <Setter Property="Foreground" Value="#575757" />
                    </Style>
                    <!-- /Important -->

                </Border.Resources>
                <ContentPresenter Content="{TemplateBinding HelpContent}" />
            </Border>
        </Popup>                                   
    </Grid>
</ControlTemplate>

重要的部分-我想为自定义样式分配给放置在弹出窗口中的项目(用作可点击的提示)

The Important part - I want to assign custom styles to items, which are being put inside the popup (it serves as a clickable hint)

我通过以下方式使用控件:

I'm using my control in the following way:

<local:MyControl>
    <local:MyControl.HelpContent>
        <TextBlock>Ala ma kota</TextBlock>
    </local:MyControl.HelpContent>
</local:MyControl>

但是,尽管边框具有样式,但 TextBlock 的文本颜色始终继承其父级的值(使用Snoop进行检查)-导致白色背景上的白色文本.

But despite styles in the Border, TextBlock's text's color always inherit the value from its parent (checked using Snoop) - resulting in white text on white background.

您可以将小PoC应用程序,演示了该问题.

我的观察:

  • 样式可以用于 Label .它仅对 TextBlock 不起作用.
  • 当我向边框添加 TextBlock.Foreground ="Red" 时, TextBlock 变为红色,仍然忽略样式(但现在使用Border的颜色).
  • Snoop通知此 TextBlock 实际上正确解析了 Style .但是尽管不应该,它使用继承的值,而不是样式中指定的值.
  • The styling does work for Label. It only doesn't work for TextBlock.
  • When I add TextBlock.Foreground="Red" to the Border, TextBlock becomes red, still ignoring style (but now using color from Border).
  • Snoop informs, that this TextBlock actually has the Style resolved correctly. But despite it shouldn't, it uses the inherited value instead of one specified in the style.

我该如何解决这个问题,为什么会发生呢?

How can I solve this problem and why does it occur?

推荐答案

我在Microsoft论坛上收到了答案;如果有人遇到同样的问题,我会把它留在这里.

I received answer on Microsoft forums; I'll leave it here in case someone encounters the same problem.

区别在于TextBlock不是控件,即它没有任何ControlTemplate,因此,当它位于StackPanel内部时,隐式样式不会应用到该控件.请参阅以下页面以获取更多信息: http://blogs.msdn.com/b/wpfsdk/archive/2009/08/27/implicit-styles-templates-controls-and-frameworkelements.aspx

您可以使用Label元素或显式设置TextBlock元素的样式.

You could use Label elements or set the style for the TextBlock elements explicitly.

-马格努斯(MM8)

这篇关于WPF,样式未应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆