使用样式设置WPF控件背景图像?

avi*_*viv 7 wpf resources styles background-image

我在堆栈面板中有一组按钮.我希望他们都有背景图片.我怎么能用风格呢?因为我不想手动设置每个按钮的背景图像.

这是一段代码:

    <StackPanel Orientation="Horizontal" Height="100px" VerticalAlignment="Top">
        <StackPanel.Resources>
            <Style TargetType="Button">
                <Setter Property="Margin" Value="2,4" />
            </Style>
        </StackPanel.Resources>
        <Button Width="127px" Height="79px" VerticalAlignment="Bottom">
            <Button.Background>
                <ImageBrush ImageSource="images/Tab.png" />
            </Button.Background>
        </Button>
        <Button>A</Button>
        <Button>R</Button>
        <Button>S</Button>
    </StackPanel>
Run Code Online (Sandbox Code Playgroud)

谢谢.

luv*_*ere 16

好吧,你Background在样式中为属性指定了一个setter ,并将其值设置为ImageBrush.

<StackPanel Orientation="Horizontal" Height="100px" VerticalAlignment="Top">
        <StackPanel.Resources>
            <Style TargetType="Button">
                <Setter Property="Margin" Value="2,4"/>
                <Setter Property="Background">
                  <Setter.Value>
                    <ImageBrush ImageSource="images/Tab.png"/>
                  </Setter.Value>
                </Setter>
            </Style>
        </StackPanel.Resources>

        <Button Width="127px" Height="79px" VerticalAlignment="Bottom"/>
        <Button>A</Button>
        <Button>R</Button>
        <Button>S</Button>
    </StackPanel>
Run Code Online (Sandbox Code Playgroud)