【发布时间】:2023-03-14 12:34:01
【问题描述】:
我陷入了一个非常奇怪的问题,我找不到解决方案。我尝试了几乎所有可能的方法(甚至删除并重新创建解决方案)来解决这个问题。
问题
我正在尝试覆盖 WPF 应用程序中的默认控件样式。我在App.xaml 中定义了所有资源和样式。问题是,按钮前景色没有覆盖运行时。令人惊讶的是,它在 VS 设计器中显示覆盖的颜色(白色),但是当我运行应用程序时,它变成黑色(可能是默认值)。
应用中没有其他代码可以更改按钮样式。
附加信息:按钮位于用户控件中,并且加载在 MainWindow 的 ContentControl 上。
<SolidColorBrush x:Key="WhiteBrush" Color="#FFFFFF"/>
<SolidColorBrush x:Key="BlackBrush" Color="#000000"/>
<SolidColorBrush x:Key="ActiveBrush" Color="#3A71C5"/>
<SolidColorBrush x:Key="HoverBrush" Color="#0071C5"/>
<SolidColorBrush x:Key="PressBrush" Color="#3A8BF4"/>
<SolidColorBrush x:Key="DefaultBrush" Color="#0071F9"/>
<SolidColorBrush x:Key="DisabledBrush" Color="#F4F4F4"/>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="{StaticResource ActiveBrush}"/>
<Setter Property="Foreground" Value="{StaticResource WhiteBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" BorderBrush="Transparent" BorderThickness="0">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource HoverBrush}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{StaticResource PressBrush}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{StaticResource DisabledBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
VS Designer 中的按钮
应用运行时中的按钮
【问题讨论】:
标签: c# wpf xaml wpf-style app.xaml