Андрей Про
Андрей Про

Reputation: 799

Background Image of Button not showing in WPF

I have program in which I have lots of buttons. Each of button has background set as

  <Button x:Name="mybutton"  HorizontalAlignment="Left" Height="30" Margin="76,110,0,0" VerticalAlignment="Top" Width="25" Click="some_click">
                <Button.Background>
                    <ImageBrush ImageSource="Resource/button_picture.png"/>
                </Button.Background>
</Button>

Image is showing as background in .xaml when program not running but when i run application image is not there as background of button. How do i debug this background in button ? Is there any goofy error that is there?

Upvotes: 5

Views: 12583

Answers (3)

Shrinand
Shrinand

Reputation: 351

Let's make sure we have the following properties set right in your scenario

1) Build Action -> Resource

2) Copy to Output Directory -> Do not copy

3) Instead of using the relative path for image source, try using a full path to the image like this (I say this because I don't know where the image resource is located in your project, using relative path is perfectly normal in WPF)

<Image Source="pack://application:,,,/AssemblyNameContainingImageResource;component/Resource/button_picture.png" />

Upvotes: 6

Manish
Manish

Reputation: 510

Change the Build Action of button_picture.png to Resource if it is content. Also check the Copy to output directory property value

Upvotes: 0

Herks
Herks

Reputation: 897

You need to change the code like this

<Button x:Name="mybutton"  HorizontalAlignment="Left" Height="30" Margin="76,110,0,0"    VerticalAlignment="Top" Width="25" Click="some_click">
    <Image Source="Resource/button_picture.png"/>
</Button>

Upvotes: 3

Related Questions