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

时间:2021-10-21 22:43:57

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.

谢谢。

2 个解决方案

#1


14  

Well, you specify a setter for the Background property 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>

#2


1  

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>

#1


14  

Well, you specify a setter for the Background property 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>

#2


1  

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>