C# – Image in WPF Button not Visible at Runtime

buttoncimagewpf

All, I have the following start to a small application that checks .resx files for consistency of embedded brackets (so that runtime errors of non-matching "… {0}" strings don't happen). I have the following XAML for the MainWindow.xaml, and my particular problem relates to the image that is to be displayed on the button

<Window x:Class="ResxChecker.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="174.383" Width="495.869">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="350*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="30*"/>
        </Grid.RowDefinitions>
        <Label Content="Select .resx file:" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Height="24" Width="Auto" Grid.ColumnSpan="1"/>
        <TextBox Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Margin="10,0,0,0" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/>
        <Button Grid.Column="2" HorizontalAlignment="Right" Margin="5,0,10,0" Grid.Row="1">
            <Image VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="16 " Width="16" Source="pack://siteoforigin:,,,/Resources/UserCost2013Open16.png"/>
        </Button>
    </Grid>
</Window>

The image has 'Build Action = Resource', 'Copy to output directory = Do not copy' – the image shows in the designer but not at runtime. I have seen the following questions and read the relevant answers, but none resolve the problem:

  1. WPF control images not displayed when consumed by an application

  2. image problem in wpf (image does not show up)

  3. Background Image of Button not showing in WPF

How do I get the button image to appear at runtime?

Best Answer

Change the build action to 'Resource'. Also your pack url is wrong. Either use:

Source="pack://application:,,,/Resource/UserCost2013Open16.png"

or simply

Source="/Resource/UserCost2013Open16.png"
Related Topic