[英]A Control does not use a defined Style but another does
我對 XAML 中的 Styles 有疑問,也許你能幫我。
我創建了一個 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>
比我制作了另外兩個 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>
另一個使用 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>
在按鈕上使用按鈕樣式時,一切都按預期工作,colors 根據需要更改,但文本塊不會更改背景,我不明白為什么。 Textblock 和 Button 應該看起來相同(對於背景和前景)
有什么結論嗎?
親切的問候米爾科
編輯:左正確,右背景應該是藍色而不是白色。
更改字體族(組合框)和大小(滑塊控制)對按鈕和文本塊都有效。
TextBlock 不從控件繼承。
看看 inheritance 鏈:
https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.textblock?view=netframework-4.8
Object DispatcherObject DependencyObject Visual UIElement FrameworkElement TextBlock
這就是為什么您的基本樣式定位控件不會應用於文本塊的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.