Wpf button background image

I have a wpf page with 5 buttons. All 5 buttons should be the same size, and they should all have the same background image. How to do it?

Regards, Karn

+3
source share
1 answer

Use style. This automatically applies to all buttons in the window:

<Window.Resources>
    <Style TargetType="Button">
        <Setter Property="Width" Value="100" />
        <Setter Property="Height" Value="100" />
        <Setter Property="Background">
            <Setter.Value>
                <ImageBrush ImageSource="ButtonBg.png" />
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

Sets the default values ​​for width, height, and background for all buttons. It is assumed that you have a bitmap called ButtonBg.png compiled into your project - this is a background image.

If you want to apply this style only to certain buttons, you must change the first line this way:

<Style x:Key="imgBtnStyle" TargetType="Button">

, , :

<Button Style="{StaticResource imgBtnStyle}" ... />

, Visual Studio 2010, . . , .

+7

Source: https://habr.com/ru/post/1773099/


All Articles