无法覆盖由TargetType在单个特定控件上设置的全局WPF样式

Mat*_* H. 8 wpf user-controls styles font-size font-family

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

<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>\
Run Code Online (Sandbox Code Playgroud)

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

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

<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>
Run Code Online (Sandbox Code Playgroud)

......等......

         <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>
Run Code Online (Sandbox Code Playgroud)

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

正如您所看到的,我展示了我在上面的XAML示例中尝试过的所有内容......

我错过了什么?

Mat*_* H. 1

好吧,我明白现在发生了什么。

在我的 UserControl 的构造函数中,我什至尝试设置 .Style=Nothing,只是为了看看会发生什么......而我的全局样式仍在出现并应用自身。

我必须假设全局样式在构造函数运行后应用于我的控件,因此全局样式无论如何都会侵入。最后,我只是将“全局”样式移动到一个仅会影响我需要的位置。

不太方便,但至少我可以继续我的项目