path.fill xaml 中的模板绑定

path.fill templatebinding in xaml

下面的 xaml 工作正常:

    <Style x:Key="TopButton" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Path Data="{StaticResource TopCube}" >
                        <Path.Style>
                            <Style>
                                <Setter Property="Path.Fill" Value="#414042" />
                                <Style.Triggers>
                                    <Trigger Property="Canvas.IsMouseOver" Value="True">
                                        <Setter Property="Path.Fill" Value="{StaticResource HoveredGradient}" />
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Path.Style>
                    </Path>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

但是,当我将 Value="#414042" 替换为 Value="{TemplateBinding Background}" 时,出现错误“无法识别背景。当我将其更改为 Value="{TemplateBinding Button.Background}" 时,它编译正常,但我在 Setter.Value

上得到运行时 XAMLparseException

我在这里错过了什么?

在这种情况下,如果你想获得默认 Button.Background,你需要使用 TemplatedParent 绑定

<Setter Property="Path.Fill" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />