Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In WPF XAML how can I concatenate 2 constants so I can use pre-define paths?

Tags:

c#

wpf

xaml

I want to replace the path part of the source path with a c# constant for easier path management, for example I have:

<Image Source="/Images/Themes/Buttons/MyPicture.png" />

and in another class I have my constant defined:

public static readonly string UriImagesButtons = "/Images/Big/PNG/";

I want to have something along the lines of:

<Image Source="{static:UriImagesButtons + MyPicture.png}" />

This means that I can change the path globally at a stroke if the need arises. What's the syntax to do this?

like image 400
Philip Avatar asked Mar 13 '10 14:03

Philip


2 Answers

The easiest way to do this is with a MultiBinding with a StringFormat:

<Path>
    <Path.Source>
        <MultiBinding StringFormat="{}{0}{1}">
            <Binding Mode="OneTime" Source="{x:Static lcl:ConstantOwner.UriImagesButtons}" />
            <Binding Mode="OneTime" Source="MyPicture.png" />
        </MultiBinding>
    </Path.Source>
</Path>

In the StringFormat parameter, you have to escape the first curly brace with a "{}" so the XAML parser doesn't think it is a markup extension.

like image 65
Abe Heidebrecht Avatar answered Oct 24 '22 12:10

Abe Heidebrecht


You should take a look at the discussion on this thread: string manipulation in xaml attribute

Basically, you can create your own markup extension that takes 2 parameters and concatenates them. This particular example is also used for binding. I suspect you'd like to be able to bind to where you have the root path defined, or some other data perhaps.

like image 21
Benny Jobigan Avatar answered Oct 24 '22 10:10

Benny Jobigan