无法覆盖由TargetType在单个特定控件上设置的全局WPF样式 [英] Can't override a global WPF style that is set by TargetType on a single specific control

查看:180
本文介绍了无法覆盖由TargetType在单个特定控件上设置的全局WPF样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在资源字典中定义了一种应用于所有文本框的样式.

I have a style applied to all my textboxes, defined in a resource dictionary..

<Style TargetType="TextBlock">
        <Setter Property="TextBlock.FontSize" Value="{Binding Source={StaticResource ApplicationUserSettings}, Path=fontSize, Mode=OneWay}" />
        <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
        <Setter Property="TextBlock.VerticalAlignment" Value="Center"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="TextBox.FontFamily" Value="{Binding Source={StaticResource ApplicationUserSettings}, Path=fontName, Mode=OneWay}"/>
    </Style>\

fontsize和fontstyle属性绑定到实现iNotifyPropertyChanged的特殊用户设置类,该类允许对字体大小和fontfamily进行更改以立即在整个应用程序中传播.

The fontsize and fontstyle properties are bound to a special user settings class that implements iNotifyPropertyChanged, which allows changes to font size and fontfamily to immediately propogate throughout my application.

但是,在我创建的UserControl中(具有讽刺意味的是,该屏幕允许用户自定义其字体设置),我希望字体大小和fontfamily保持静态.无论我尝试什么,我的全局字体设置都会覆盖我在用户控件中设置的内容:

However, in a UserControl I've created (Ironically, the screen that allows the user to customize their font settings), I want the font size and fontfamily to remain static. No matter what I try, my global font settings override what I set in my user control:

<UserControl x:Class="ctlUserSettings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:R2D2" Height="400" Width="600">

<Grid>

    <Grid.Resources>
        <Style x:Key="tbxStyle" TargetType="TextBox">
            <Style.Setters>
                <Setter Property="FontSize" Value="14"/>
                <Setter Property="FontFamily" Value="Tahoma"/>
            </Style.Setters>
        </Style>

...等等...

         <StackPanel Margin="139,122.943,41,0" Orientation="Horizontal" Height="33" VerticalAlignment="Top">
            <TextBox Style="{x:Null}" FontSize="13" FontFamily="Tahoma" HorizontalAlignment="Left" MaxWidth="500" MinWidth="350" Name="txtReaderPath" Height="Auto" VerticalAlignment="Top" />
            <TextBox Style="{x:tbxStyle}" Margin="15,0,0,0" HorizontalAlignment="Left" Name="txtPath" Width="43" Height="23" VerticalAlignment="Top">(some text)</Button>
        </StackPanel>

我尝试将样式设置为{x:Null},内联设置自定义字体大小,并在此控件的资源中设置样式.没有一个优先于我的资源字典中的样式.

I've tried setting Style to {x:Null}, setting custom font sizes inline, and setting a style in the resources of this control. None take precedence over the styles in my resource dictionary.

如您所见,我在上面的XAML示例中展示了我尝试过的所有内容……

As you can see, I show a sprinkling of all the things I've tried in the XAML sample above...

我想念什么?

推荐答案

另一种替代方法是在UserControl上添加空样式,例如:

Another alternative is to add an empty style on the UserControl such as:

<UserControl.Resources>
        <Style TargetType="TextBlock" />
</UserControl.Resources>

这会阻止全局样式-也可以将其添加到StackPanel资源中以进行更精细的控制

That blocks the global style - can also add it to the StackPanel resources for finer grained control

这篇关于无法覆盖由TargetType在单个特定控件上设置的全局WPF样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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