无法覆盖由 TargetType 在单个特定控件上设置的全局 WPF 样式
我在资源字典中定义了应用于所有文本框的样式。fontsize
<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>\
和 fontstyle 属性绑定到实现 iNotifyPropertyChanged 的特殊用户设置类,该类允许对字体大小和 fontfamily 的更改立即传播到我的应用程序中。
但是,在我创建的 UserControl 中(具有讽刺意味的是,该屏幕允许用户自定义其字体设置),我希望字体大小和字体系列保持静态。无论我尝试什么,我的全局字体设置都会覆盖我在用户控件中设置的内容:
<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>
我尝试将 Style 设置为 {x:Null},内联设置自定义字体大小,并在此控件的资源中设置样式。没有一个优先于我的资源字典中的样式。
正如您所看到的,我在上面的 XAML 示例中展示了我尝试过的所有内容...
我缺少什么?
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>\
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.
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>
... etc...
<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>
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.
As you can see, I show a sprinkling of all the things I've tried in the XAML sample above...
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另一种选择是在 UserControl 上添加空样式,例如:
阻止全局样式 - 也可以将其添加到 StackPanel 资源中以进行更细粒度的控制
Another alternative is to add an empty style on the UserControl such as:
That blocks the global style - can also add it to the StackPanel resources for finer grained control
好吧,我明白现在发生了什么。
在我的 UserControl 的构造函数中,我什至尝试设置 .Style=Nothing,只是为了看看会发生什么......而我的全局样式仍在出现并应用自身。
我必须假设全局样式在构造函数运行后应用于我的控件,因此全局样式无论如何都会侵入。最后,我只是将“全局”样式移动到一个仅会影响我需要的位置。
不太方便,但至少我可以继续我的项目
Ok I see what is happening now.
In the constructor for my UserControl, I even tried setting .Style=Nothing, just to see what would happen... and my global style was STILL coming along and applying itself.
I've got to assume that the global style is being applied to my controls AFTER the constructor runs, and as such the global style intrudes anyway. In the end, I've simply moved the 'global' style to a location where it will only effect what I need it to.
Less convenient, but at least I can move on in my project