user3356236
user3356236

Reputation: 102

XAML Image Resource

I add some pictures as ressource to my project (for example I put them into the folder "ressources"). Now I'm using them with:

<Image Name="_ImageName" Width="100" Height="100" Source="Resources\HalloPicA.png"/>
<Image Name="_ImageName" Width="100" Height="100" Source="Resources\HalloPicB.png"/>

... and so one.

Imagine now I will change the folder "Resources" to "MyResources". The Problem I have to change it everywhere in the XAML-code. Is there a better way existing? In c#-Code I would declare a member variable from typ string like:

privat _FolderName = "Resources";

and use this member variable in this way:

Image newImage = Image.FromFile(_FolderName +"HalloPicA.png");

Hopefully my problems are clear.

Upvotes: 1

Views: 4273

Answers (2)

dkozl
dkozl

Reputation: 33394

What you can do is to create separate resource dictionary, or put it in Application.Resources, with BitmapImage resources:

<Application ...>
    <Application.Resources>
        <BitmapImage UriSource="/MyAssemblyName;component/MyPath/myImage.png" x:Key="imageKey"/>
    </Application.Resources>
</Application>

and then when you want to use it

<Image Width="100" Height="100" Source="{StaticResource imageKey}"/>

this way when folder changes you'll need to change it only in one file

Upvotes: 1

rPulvi
rPulvi

Reputation: 946

You can use Property Binding and Value Converters to do this. For example:

    <Image Name="_ImageName" Width="100" Height="100" Source="{Binding Path=imgA, Converter={StaticResource imgPathConverter}}" />

Where your imgPathConverter is the class that can convert image name in your "qualified" image path.

Upvotes: 2

Related Questions