In my application I am creating a ton of custom controls that I'm then using in different windows. The structure of my project looks something like this:
- Controls (folder)
- MemberList (class)
- Resources (folder)
- Windows (folder)
- Window1.xaml
- Window2.xaml
- Helper windows (folder)
- Window3.xaml
I am creating classes (controls) that I store in the Controls folder. If I want to use an image in the control, I access it like so:
new Uri("../Resources/my_image.png", UriKind.Relative)
Sometimes though, I want to use the same control in multiple windows and sometimes these windows are stored in different folders. If I use the control in say Window3 that is stored in another folder, the image is not displayed.
If I change the Uri to this: (adding another "go back" command in the path)
new Uri("../../Resources/my_image.png", UriKind.Relative)
the image is being displayed in the Window3, but it is not displayed in the Window1 or Window2.
Is there a way to create paths that are relative to the main executable and not relative to the window that is displaying the control?
EDIT: I forgot to mention one important part. Because of the fact that while starting this application I was just learning how to use WPF, I messed one thing. My resources folder is not in the folder that contains the main executable.
My file structure looks like this:
- bin
- Debug
- MyApplication.exe
- Debug
- Resources
I was trying to use the path that everyone suggested:
new Uri("pack://application:,,,/../../my_image.png", UriKind.RelativeOrAbsolute)
but I get the exception:
Cannot locate resource 'my_image.png'.
I could try to relocate the Resources folder, but it would mess up all other paths. Is there any way to go "back" in folder structure in the "pack" uri? If not, what would be the best way to relocate a folder so all paths do not get messed up?
UriKind.Relative
on a Pack URI. None of the three answers is actually giving you an appropriate example. – Tripartedpack://application:,,,/../../my_image.png
is an invalid Pack URI. Besides that, better do not specify an UriKind at all. – Triparted