我有一个 View 框:
<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>
现在我用 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>
但是,只有一个按钮有椭圆(viewbox)
如何让所有的按钮都有/显示椭圆??
最佳答案
Viewbox 是不能属于多个父级的 FrameworkElement。每次按钮请求资源 {StaticResource SampleViewbox}
时,它们都会获得相同的实例。
要更改该行为,请添加 x:Shared="False"
属性
<Viewbox x:Key="SampleViewbox" x:Shared="False">
关于c# - Button Style 的内容只出现在一个 Button 实例中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42924151/