样式按钮图像仅适用于最后一项

Joh*_*las 2 wpf

我有一种风格

<Style x:Key="HomeButtonStyle" TargetType="Button">
    <Setter Property="Height" Value="32"/>
    <Setter Property="Width" Value="32"></Setter>
    <Setter Property="Content">
        <Setter.Value>
            <Image Source="/Images/HOME.png"></Image>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

我在控件中有一些xaml

    <StackPanel Grid.Row="0" Orientation="Horizontal">
        <Button Style="{DynamicResource HomeButtonStyle}" />
        <Button Style="{DynamicResource HomeButtonStyle}" />
        <Button Style="{DynamicResource HomeButtonStyle}" />
        <Button Style="{DynamicResource HomeButtonStyle}" />
    </StackPanel>
Run Code Online (Sandbox Code Playgroud)

但是,图像仅显示在最后一个按钮中.

我的问题是为什么?以及如何在所有按钮中显示图像?

idu*_*sun 6

在你的风格,你应该ContentTemplate而不是Content直接设置.

<Setter Property="ContentTemplate">
  <Setter.Value>
     <DataTemplate>
       <Image Source="/Images/HOME.png"></Image>
     </DataTemplate>
    </Setter.Value>
</Setter>
Run Code Online (Sandbox Code Playgroud)