如果已设置样式,如何覆盖 WPF 子控件样式?

Howto override WPF childcontrols style if a style already is set?

如果我有一个简单的子控件,它已经为子控件中的元素定义了样式。 我可以从父控件更改该元素的样式吗?

如果我没有在子控件中设置样式,我可以从父控件中覆盖它,但是当设置了样式时我似乎无法让它工作。

我无法更改子控件中的代码。

示例子控件:

    <UserControl
    x:Class="TestingThemes.ChildControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:TestingThemes"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d">
    <UserControl.Resources>
        <Style x:Key="aw" TargetType="Button">
            <Setter Property="Foreground" Value="Blue" />
        </Style>
    </UserControl.Resources>
    <Grid>
        <Button
            Width="100"
            Height="50"
            Content="Kalle Anka"
            <!-- If I remove the style I can override it from parent control -->
            Style="{DynamicResource aw}" />
    </Grid>
</UserControl>

父控件:

 <Grid>
    ...            
    <local:ChildControl>
        <local:ChildControl.Resources>
            <!--<Style BasedOn="{StaticResource local:ResourceKey=aw}" TargetType="Button">-->
            <Style TargetType="Button">
                <Setter Property="Foreground" Value="Red" />
            </Style>
        </local:ChildControl.Resources>
    </local:ChildControl>
   
</Grid>

这是可能的,但您必须更改初始位置 Style。 XAML 引擎将遍历逻辑树以从本地开始查找元素(例如 Button)的 StaticResource/DynamicResource 资源,本地 ResourceDictionary

这意味着,您希望在查找路由的最后找到在 UserControl 中定义的默认值 Style - 在任何旨在覆盖的潜在自定义样式之后默认 Style。为此,您必须将 Style 移动到 App.xamlGeneric.xaml.
此外,引用必须是 DynamicResource 以便将查找推迟到逻辑树组成的那一刻。覆盖 Style 可以在通往树根的路线上的任何地方定义。

由于 Generic.xaml 提供了不同的名称范围,您必须定义一个唯一的静态(就 XAML 名称范围而言是全局的)ComponentResourceKey 您必须将其用作样式的 x:Key 值(如果您计划将默认样式移动到 Generic.xaml 文件)。

由于上述 XAML 资源查找行为,创建自定义 Control 总是优于 UserControl 当您计划使控件可自定义时(就像您的情况一样) .自定义 Control 的默认 Style 默认位于 Generic.xaml,因此您永远不会 运行 进入您当前的问题。

ChildControl.xaml

<UserControl>
  <Grid>
    <Button Style="{DynamicResource aw}" />
  </Grid>
</UserControl>

App.xaml
Generic.xaml之前的最后一个查找位置。如果尚未找到具有相同密钥的 Style,将应用在 App.xaml 中找到的 Style

<Style x:Key="aw" TargetType="Button">
  <Setter Property="Foreground" Value="Blue" />
</Style>

ParentControl.xaml

<ChildControl>
  <ChildControl.Resources>
    <Style x:Key="aw" TargetType="Button">
      <Setter Property="Foreground" Value="Red" />
    </Style>
  </ChildControl.Resources>
</ChildControl>