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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2827473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:45:16  来源:igfitidea点击:

Setting WPF control background image using styles?

wpfresourcesstylesbackground-image

提问by aviv

I have a set of buttons inside a stack panel. I want them all to have a background image. How can i do it using styles? since i don't want to set manually the Background image for each button.

我在堆栈面板中有一组按钮。我希望他们都有一个背景图片。我如何使用样式来做到这一点?因为我不想为每个按钮手动设置背景图像。

Here is a code snippet:

这是一个代码片段:

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

Thanks.

谢谢。

回答by luvieere

Well, you specify a setter for the Backgroundproperty within the style, and you set its value to the ImageBrush.

好吧,您为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>

回答by Malcolm

Below is the style for your button and backgroung image is set to it.You can change the source of the ImageBrush to the one which you wanted .

下面是按钮的样式,背景图像设置为它。您可以将 ImageBrush 的源更改为您想要的源。

<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
            <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
            <Setter Property="Background" >
                <Setter.Value>
                    <ImageBrush ImageSource="pic.png"></ImageBrush>
                </Setter.Value>
            </Setter>

            <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Microsoft_Windows_Themes:ButtonChrome>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsKeyboardFocused" Value="true">
                                <Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/>
                            </Trigger>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="#ADADAD"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

And then just use it:

然后只需使用它:

<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"  Style="{StaticResource ButtonStyle1}">
        </Button>
        <Button  Style="{StaticResource ButtonStyle1}" >A</Button>
        <Button  Style="{StaticResource ButtonStyle1}">R</Button>
        <Button  Style="{StaticResource ButtonStyle1}">S</Button>
    </StackPanel>