WPF Button style won't apply to second button

2019-08-03 21:49发布

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?

标签: wpf wpf-style
1条回答
对你真心纯属浪费
2楼-- · 2019-08-03 22:24

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.

查看更多
登录 后发表回答