WPF Button style won't apply to second button

孤街浪徒 提交于 2020-01-06 02:38:22

问题


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}"/>

The style is very basic:

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


回答1:


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.



来源:https://stackoverflow.com/questions/51307989/wpf-button-style-wont-apply-to-second-button

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!