WPF按钮样式不适用于第二个按钮 [英] WPF Button style won't apply to second button

查看:133
本文介绍了WPF按钮样式不适用于第二个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个使用静态通用样式的按钮:

I have two buttons that use a static common style:

<Button x:Name="BtnCreate" Height="22" Width="150" Style="{StaticResource Style.Button.Trash}"/>
<Button x:Name="aefae" Height="22" Width="150" Style="{StaticResource Style.Button.Trash}"/>

样式非常基本:

<Style TargetType="Button" x:Key="Style.Button.Trash" 
       BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="Content">
        <Setter.Value>
        <StackPanel Orientation="Horizontal" >
            <Image Source="{StaticResource Image.Trash}" Width="22" Height="22"/>
            <Label Content="Save" VerticalAlignment="Center" Height="26" />
        </StackPanel>
        </Setter.Value>
    </Setter>
</Style>

样式适用于第一个按钮,并同时显示图像和文本,但是第二个按钮除灰色按钮外不显示其他任何内容.

The style applies to the first button and display both the image an the text however the second button does not display anything except a grey button.

为什么第二个按钮不使用静态样式?

Why does the second button not use the static style?

推荐答案

当您尝试将相同的Content添加到两个Button时,不能将元素(显示在Style中)添加到两个不同的元素中逻辑父母.为避免这种情况,可以将x:Shared="False"设置为样式.

As you're trying to add the same Content to two Buttons, elements (present in Style) cannot be added to two different Logical Parents. To avoid this, you can set x:Shared="False" to your style.

这篇关于WPF按钮样式不适用于第二个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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