相关疑难解决方法(0)

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

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

<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},设置内联自定义字体大小,并在此控件的资源中设置样式.没有优先于我的资源字典中的样式. …

wpf user-controls styles font-size font-family

8
推荐指数
1
解决办法
4157
查看次数

标签 统计

font-family ×1

font-size ×1

styles ×1

user-controls ×1

wpf ×1