【问题标题】:Content of a Button Style appears only in one Button instanceButton Style 的内容只出现在一个 Button 实例中
【发布时间】:2017-08-12 22:39:38
【问题描述】:

我有一个 Viewbox:

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

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

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

【问题讨论】:

    标签: c# .net wpf data-binding wpf-controls


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢你成功了。我曾在 Kaxaml 中尝试过,但它引发了一些错误。现在在 Visual Studio 中试了一下,效果很好。 x:Shared="False" 标志有什么缺点吗?
    • @Sabz,缺点?默认是x:Shared="True",在大多数情况下不想创建对象的多个副本(例如画笔资源、转换器)。但这是相反的情况,这是期望的行为
    【解决方案2】:

    我认为一个好的方法是使用 DataTemplate。 所以你会有

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

    为了风格

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

    【讨论】:

    • 完美。希望我也可以将此标记为答案。
    【解决方案3】:

    按照@ASh 的建议将ViewBoxx:Shared 属性设置为false 确实可行,但为什么不像这样在ControlTemplate 中包含ViewBox

    <Style x:Key="SampleStyle" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Background="Transparent" >
                        <Viewbox>
                            <Grid>
                                <Ellipse Stroke="#e2e2e0" StrokeThickness="6" Fill="#d5273e" Width="128" Height="128"/>
                            </Grid>
                        </Viewbox>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    模板就是模板,控件实例就是实例。

    【讨论】:

    • 为简单起见,我只写了一个椭圆。在实际情况下,Viewbox 包含其他复杂的图形。我也想在其他样式中使用该视图框..
    • 我明白了,很公平:)
    猜你喜欢
    • 2021-05-24
    • 1970-01-01
    • 2020-12-21
    • 2011-11-22
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 2021-01-29
    相关资源
    最近更新 更多