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

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

谢谢。

最佳答案

好了,您为样式中的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>

关于wpf - 使用样式设置WPF控件背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2827473/

相关文章:

c# - Entity Framework 不插入记录

python - Python 中的 'ulimit' 是什么?

jquery - 根据内容覆盖 td

android - 为什么在使用 InstrumentationRegistry.getContext().getString() 时会出现 Resources$NotFoundException?

wpf - 在我的类库项目中使用资源字典样式而不合并字典

wpf - 如何将控件的属性 "reset"为其原始样式值(例如Background)

android - ICS 中似乎被忽略的主题

c# - 如何在右键单击时获取数据网格列索引? MVVM WPF

c# - C# 使用和 Java 导入之间的区别

c# - ContentControl 不显示内容