Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In WPF how do I specify the path to a file nested in a Directory using XAML?

Tags:

file

path

wpf

xaml

I am trying to do this...

<Image x:Name="imgGroupImage" Source="Images\unlock.png" Margin="0,0,5,0" />

But I get this error...

Cannot convert string 'Images\unlock.png' in attribute 'Source' to object of type 'System.Windows.Media.ImageSource'. Cannot locate resource 'forms/images/unlock.png'. Error at object 'System.Windows.HierarchicalDataTemplate' in markup file 'Fuse;component/forms/mainwindow.xaml' Line 273 Position 51.

As you can see, my form that includes this XAML is in a folder named Forms. My Images are in a folder named Images. How do I map from Forms to Images?

I tried Source="..Images\unlock.png" which does not work in WPF.

Any help?

like image 821
Doug Avatar asked Nov 10 '08 19:11

Doug


People also ask

How do you set the path of a file?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

How do I use namespace in XAML?

A XAML namespace is really an extension of the concept of an XML namespace. The techniques of specifying a XAML namespace rely on the XML namespace syntax, the convention of using URIs as namespace identifiers, using prefixes to provide a means to reference multiple namespaces from the same markup source, and so on.

What is path data in WPF?

The Path object represents a path shape and draws a path. The Path object draws both closed and open paths. A closed path is a shape that has the same start and end points and an open path is a shape that has different start and end points. The Fill property fills the interior of an ellipse.

What is folder paths?

A path is a slash-separated list of directory names followed by either a directory name or a file name. A directory is the same as a folder.


1 Answers

Try slashes rather than backslashes, and use an absolute path by leading with a slash:

Source="/Images/unlock.png"

That generally works for me.

Failing that, take a look at Pack URIs.

like image 79
Matt Hamilton Avatar answered Oct 07 '22 21:10

Matt Hamilton