ControlTemplate 基于另一个 ControlTemplate

发布于 2024-12-02 10:29:30 字数 1729 浏览 2 评论 0原文

我知道这个问题一定有一个简单的答案,但我找不到。

我有一个名为 HoverButton 的按钮样式。

<Style x:Key="HoverButton" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border>
                    <ContentPresenter/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="BorderBrush" Value="Black"/>
                        <Setter Property="Background" Value="WhiteSmoke"/>
                        <Setter Property="Foreground" Value="DarkRed"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="Background" Value="Transparent"/>
</Style>

然后,我想创建另一个基于具有特定内容的 HoverButton 的“派生”样式。我在这里降低了上述样式的复杂性,但它已经足够复杂了,我不想复制和粘贴它。

<Style x:Key="CloseButton" BasedOn="{StaticResource HoverButton}" TargetType="{x:Type Button}">
    <Setter Property="Content">
        <Setter.Value>
            <Path Width="8" Height="8" Stroke="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType=Button}}" Data="M0,0 L8,8 M8,0 L0,8" StrokeThickness="2" />
        </Setter.Value>
    </Setter>
</Style>

这不起作用 - “指定的元素已经是另一个元素的逻辑子元素。首先断开它。”看来我需要重新定义派生样式的 Template 属性,但以某种方式引用基本样式的模板。

有什么想法吗?

I know there must be an easy answer to this but I can't find it.

I've got a button style called HoverButton.

<Style x:Key="HoverButton" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border>
                    <ContentPresenter/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="BorderBrush" Value="Black"/>
                        <Setter Property="Background" Value="WhiteSmoke"/>
                        <Setter Property="Foreground" Value="DarkRed"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="Background" Value="Transparent"/>
</Style>

I then want to create another 'derived' style that is based on a HoverButton with specific content. I've reduced the complexity of the above style here, but it's complex enough that I don't want to copy and paste it.

<Style x:Key="CloseButton" BasedOn="{StaticResource HoverButton}" TargetType="{x:Type Button}">
    <Setter Property="Content">
        <Setter.Value>
            <Path Width="8" Height="8" Stroke="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType=Button}}" Data="M0,0 L8,8 M8,0 L0,8" StrokeThickness="2" />
        </Setter.Value>
    </Setter>
</Style>

This doesn't work - "Specified element is already the logical child of another element. Disconnect it first." It seems like I need to redefine the Template property of the derived style, but somehow reference the base style's template.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜中书 2024-12-09 10:29:30

您不能将模板建立在另一个基础上,但这一错误很容易解决。只需创建一个等效的 ContentTemplate 而不是设置 Content。这样,就会为每个按钮创建一个 Path,而不是为所有按钮创建一个路径(这是不允许的)。

You cannot base templates on one-another, but this error could easily be resolved. Just create an equivalent ContentTemplate instead of setting the Content. That way one Path is created for each button, and not one for all buttons (which is not allowed).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文