XAML의 스타일에 문제가 있습니다. 도움이 될 수 있습니다.
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>
버튼과 controldefaultstyle을 기반으로 하는 두 개의 다른 ResourceDictionaries를 만든 것보다:
<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>
버튼에서 버튼 스타일을 사용할 때 모든 것이 예상대로 작동하고 색상이 원하는 대로 변경되지만 텍스트 블록은 배경을 변경하지 않으며 이유를 알 수 없습니다. 텍스트 블록과 버튼은 동일하게 보여야 합니다(배경 및 전경의 경우).
어떤 결론?
친절한 미르코
편집: 왼쪽은 맞고 오른쪽 배경은 흰색이 아니라 파란색이어야 합니다.
글꼴 패밀리(콤보박스) 및 크기(슬라이더 제어) 변경은 버튼과 텍스트 블록 모두에서 작동합니다.
TextBlock은 컨트롤에서 상속되지 않습니다.
상속 체인을 살펴보십시오.
https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.textblock?view=netframework-4.8
Object DispatcherObject DependencyObject Visual UIElement FrameworkElement TextBlock
이것이 기본 스타일 타겟팅 컨트롤이 텍스트 블록에 적용되지 않는 이유입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다