WPF 在 TextBlock 上设置默认样式会覆盖 Label 的样式

发布于 2024-08-20 05:40:20 字数 1690 浏览 7 评论 0原文

在 TextBlock 上设置默认样式会导致 Label 和其他控件中的样式也被设置。仅当您将样式放入应用程序资源中时才会发生这种情况,当我将样式放入窗口资源中时一切都很好。

我还发现 VS 2008 Designer 和 XamlPadX 按您的预期显示标签,但只有在现实生活中执行应用程序时才会出现问题。

<Application x:Class="WpfApplication.App"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   StartupUri="Window1.xaml">
   <Application.Resources>
       <ResourceDictionary>
           <Style TargetType="TextBlock">
               <Setter Property="FontSize" Value="8"/>
           </Style>

           <Style x:Key="Title" TargetType="Label">
               <Setter Property="FontSize" Value="32"/>
           </Style>
       </ResourceDictionary>
   </Application.Resources>
</Application>

<Window x:Class="WpfApplication.Window1"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Height="300"
       Title="Window1"
       Width="300">
   <StackPanel>

       <TextBlock Text="TextBlock No Style" Style="{x:Null}"/>
       <Label Content="Label No Style" Style="{x:Null}"/>

       <TextBlock Text="Default TextBlock"/>
       <Label Content="Default Label" Style="{StaticResource Title}"/>

   </StackPanel>
</Window>

上面的代码显示:

TextBlock No Style - Default font size (As you would expect)
Label No Style - Size 5 font size (How did this happen?)
Default TextBlock - Size 5 font size (As expected by my style)
Default Label - Size 5 font size (How did this happen?)

Setting the default style on a TextBlock causes the style in the Label and other controls to be set as well. This only happens if you put the styles in the Application resources, when I place the style in the Window resources everything is fine.

I have also found that the VS 2008 Designer and XamlPadX display the Label as you would expect but the problem only occurs if you execute the application in real life.

<Application x:Class="WpfApplication.App"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   StartupUri="Window1.xaml">
   <Application.Resources>
       <ResourceDictionary>
           <Style TargetType="TextBlock">
               <Setter Property="FontSize" Value="8"/>
           </Style>

           <Style x:Key="Title" TargetType="Label">
               <Setter Property="FontSize" Value="32"/>
           </Style>
       </ResourceDictionary>
   </Application.Resources>
</Application>

<Window x:Class="WpfApplication.Window1"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Height="300"
       Title="Window1"
       Width="300">
   <StackPanel>

       <TextBlock Text="TextBlock No Style" Style="{x:Null}"/>
       <Label Content="Label No Style" Style="{x:Null}"/>

       <TextBlock Text="Default TextBlock"/>
       <Label Content="Default Label" Style="{StaticResource Title}"/>

   </StackPanel>
</Window>

The code above displays:

TextBlock No Style - Default font size (As you would expect)
Label No Style - Size 5 font size (How did this happen?)
Default TextBlock - Size 5 font size (As expected by my style)
Default Label - Size 5 font size (How did this happen?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

非要怀念 2024-08-27 05:40:20

是的,这是可以预料的;查看 Label 的默认模板,它只是一个缩进的 TextBlock。样式是继承的,因此 Label 会将 FontSize 设置为 32,但随后 TextBlock 的样式将覆盖它。如果你有的话,也会是5pt。

编辑:所以我解决这个问题的方法是创建一个名为NormalText的TextBlock虚拟子类(即一个不改变任何内容的类),然后设置其样式;这样您就不会意外拾取其他 TextBlock。

Yes, that's to be expected; look at the default template for Label, it's just an indented TextBlock. Styles are inherited, so the Label will set the FontSize to 32, but then the TextBlock's style will override that. If you just had , it'd be 5pt as well.

Edit: So the way I'd solve this, is to create a dummy subclass (i.e. a class which changes nothing) of TextBlock called NormalText, then style that; this way you won't accidentally pick up other TextBlocks.

烟雨扶苏 2024-08-27 05:40:20

另外,您可以尝试更改标签的内容,例如:

  <Label>
             <Label.Content> 
                 <TextBlock 
                  Text="{Binding Content}"    
                  FontSize="30"></TextBlock> 
             </Label.Content>
   </Label> 

Also, you can try to change the label's content like:

  <Label>
             <Label.Content> 
                 <TextBlock 
                  Text="{Binding Content}"    
                  FontSize="30"></TextBlock> 
             </Label.Content>
   </Label> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文