1

Here is my XAML code

<Button HorizontalAlignment="Left" Margin="185,0,0,0" Width="20" BorderBrush="{x:Null}" ToolTip="Search" Foreground="#FFB9B9B9">
            <Button.Background>
                <ImageBrush Stretch="UniformToFill" ImageSource="../Images/gray-classic-search-icon.png"/>
            </Button.Background>
</Button>

This is giving me this error on run project:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '34' and line position '33'.

What is causing this and how can I fix it?

Sam Alex
  • 442
  • 1
  • 6
  • 21

2 Answers2

1

Loading an image from a relative path is not an ideal approach in a WPF application, because at runtime the image file needs to reside in exactly the right relative location, be it ..\Images\ or ..\..\Images\.

You should instead set the Build Action of the image file to Resource (as shown here) and load it like this:

<ImageBrush ImageSource="/Images/gray-classic-search-icon.png" .../>

which is the short form of a WPF Resource File Pack Uri:

<ImageBrush ImageSource="pack://application:,,,/Images/gray-classic-search-icon.png" .../>
Community
  • 1
  • 1
Clemens
  • 123,504
  • 12
  • 155
  • 268
0

Change your code

<Button HorizontalAlignment="Left" Margin="185,0,0,0" Width="20" BorderBrush="{x:Null}" ToolTip="Search" Foreground="#FFB9B9B9">
            <Button.Background>
                <ImageBrush Stretch="UniformToFill" ImageSource="../../Images/gray-classic-search-icon.png"/>
            </Button.Background>
        </Button>
Sam
  • 349
  • 1
  • 2
  • 12