按钮样式的内容仅出现在一个按钮实例中

2024-04-27

我有一个视图框:

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

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

<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>

现在我创建了很多按钮样本样式

<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>

然而,Only one按钮有椭圆(视图框)

我怎样才能让所有按钮都有/显示椭圆?


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

要改变这种行为添加x:Shared="False"属性

<Viewbox x:Key="SampleViewbox" x:Shared="False">
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

按钮样式的内容仅出现在一个按钮实例中 的相关文章

随机推荐