按钮样式的内容仅出现在一个 Button 实例中 [英] Content of a Button Style appears only in one Button instance

查看:25
本文介绍了按钮样式的内容仅出现在一个 Button 实例中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图框:

<Viewbox x:Key="SampleViewbox" >
  <Grid>
    <Ellipse Stroke="#e2e2e0" StrokeThickness="6" Fill="#d5273e" Width="128" Height="128"/>
  </Grid>
</Viewbox>

然后我将其包含在如下样式中:

I then include this in a Style like:

<Style x:Key="SampleStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="Transparent" >
                    <ContentPresenter Content="{StaticResource SampleViewbox}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在我用 SampleStyle

<Grid>   
    <StackPanel>
       <Button Style="{StaticResource SampleStyle}" Height="50" Width="50"></Button>
       <Button Style="{StaticResource SampleStyle}" Height="80" Width="80"></Button>
       <Button Style="{StaticResource SampleStyle}" Height="20" Width="20"></Button>  
    </StackPanel>
</Grid>

但是,只有一个按钮具有椭圆(视图框)

However, Only one button has the Ellipse (viewbox)

如何让所有按钮都有/显示椭圆??

How can I make all the buttons have/show the ellipse??

推荐答案

Viewbox 是不能属于多个父级的 FrameworkElement.每次按钮请求资源 {StaticResource SampleViewbox} 时,它们都会获得相同的实例.

Viewbox is FrameworkElement which cannot belong to multiple parents. Every time buttons request a resource {StaticResource SampleViewbox} they get the same instance.

要更改该行为,请添加 x:Shared="False" 属性

to change that behavior add x:Shared="False" attribute

<Viewbox x:Key="SampleViewbox" x:Shared="False">

这篇关于按钮样式的内容仅出现在一个 Button 实例中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆