代码之家  ›  专栏  ›  技术社区  ›  Michael Edwards

wpf按钮样式不适用于第二个按钮

  •  1
  • Michael Edwards  · 技术社区  · 6 年前

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

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

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

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   dhilmathy    6 年前

    当你试图添加相同的 Content 两个 Button s,元素(存在于 Style )无法添加到两个不同的逻辑父级。为了避免这种情况,您可以设置 x:Shared="False" 你的风格。