[英]A Control does not use a defined Style but another does
I have a problem with Styles in XAML, and maybe you could help me.我对 XAML 中的 Styles 有疑问,也许你能帮我。
i have created a ResourceDictionary "controldefaultstyle":我创建了一个 ResourceDictionary “controldefaultstyle”:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Control}" x:Key="ControlDefaultStyle" >
<Style.Setters>
<Setter Property="FontFamily" Value="{Binding Path=FontFamily, Source={x:Static Application.Current}, UpdateSourceTrigger=PropertyChanged}"/>
<Setter Property="FontSize" Value="{Binding Path=FontSize, Source={x:Static Application.Current}, UpdateSourceTrigger=PropertyChanged}"/>
<Setter Property="Background" Value="{StaticResource SystemBackground}"/>
<Setter Property="Foreground" Value="{StaticResource SystemForeground}"/>
</Style.Setters>
</Style>
than i made two other ResourceDictionaries, One which bases on the button and the controldefaultstyle:比我制作了另外两个 ResourceDictionaries,一个基于按钮和 controldefaultstyle:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ext="clr-namespace:StyleResourceDictionariesDemo.ResourceDictionaries.Classes">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ControlDefaultStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Button}" x:Key="ButtonStyle" >
<Style.BasedOn>
<ext:MergedStyles BasedOn="{StaticResource {x:Type Button}}" MergeStyle="{StaticResource ControlDefaultStyle}"/>
</Style.BasedOn>
<Style.Setters>
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="30"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}" >
<Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" ClipToBounds="True">
<Rectangle x:Name="buttonFrame" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Stroke="{TemplateBinding Background}" RadiusX="5" RadiusY="5" StrokeThickness="1" Fill="Transparent"/>
<Rectangle x:Name="buttonBody" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Stroke="Transparent" RadiusX="5" RadiusY="5" StrokeThickness="1" Fill="{TemplateBinding Background}"/>
<TextBlock x:Name="buttonText" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{TemplateBinding Content}"
Foreground="{TemplateBinding Foreground}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource IsMouseOverBackground}"/>
<Setter Property="Foreground" Value="{StaticResource IsMouseOverForeground}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{StaticResource IsPressedBackground}"/>
<Setter Property="Foreground" Value="{StaticResource IsPressedForeground}"/>
</Trigger>
</Style.Triggers>
</Style>
and another on which uses Textblock and the ControlDefaultStyle:另一个使用 Textblock 和 ControlDefaultStyle:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ControlDefaultStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type TextBlock}" x:Key="TextBlockStyle" >
<Style.BasedOn>
<classes:MergedStyles BasedOn="{StaticResource {x:Type TextBlock}}" MergeStyle="{StaticResource ControlDefaultStyle}"/>
</Style.BasedOn>
<Style.Setters>
<Setter Property="Width" Value="200"/>
<Setter Property="Height" Value="150"/>
<Setter Property="TextAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<!--<Setter Property="Background" Value="{StaticResource TextBlockBackground}"/>-->
<!--<Setter Property="Foreground" Value="{StaticResource TextBlockForeground}"/>-->
</Style.Setters>
</Style>
when using the button style on a button, everything works as expected and the colors changes as wished, but the textblock does not change the background and i don't get why.在按钮上使用按钮样式时,一切都按预期工作,colors 根据需要更改,但文本块不会更改背景,我不明白为什么。 Textblock and Button should look the same (for background and foreground)
Textblock 和 Button 应该看起来相同(对于背景和前景)
Any Conclusion?有什么结论吗?
Kind Regards Mirko亲切的问候米尔科
edit: left correct, right background should be blue and not white.编辑:左正确,右背景应该是蓝色而不是白色。
Changing Fontfamily (Combobox) and Size (Slider controlled), are working for both the buttons and the Textblock.更改字体族(组合框)和大小(滑块控制)对按钮和文本块都有效。
TextBlock does not inherit from control. TextBlock 不从控件继承。
Take a look at the inheritance chain:看看 inheritance 链:
https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.textblock?view=netframework-4.8 https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.textblock?view=netframework-4.8
Object DispatcherObject DependencyObject Visual UIElement FrameworkElement TextBlock Object DispatcherObject DependencyObject Visual UIElement FrameworkElement TextBlock
That's why your base style targeting control will not be applied to a textblock.这就是为什么您的基本样式定位控件不会应用于文本块的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.