ControlTemplate 基于另一个 ControlTemplate
我知道这个问题一定有一个简单的答案,但我找不到。
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能将模板建立在另一个基础上,但这一错误很容易解决。只需创建一个等效的
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 theContent
. That way onePath
is created for each button, and not one for all buttons (which is not allowed).