Skip to content

Using ConverterParameter to store string value for further value binding

October 30, 2015

A useful thing that I found when starting to use WPF is that you can use the NameToPathConverter to pass in string values to the NameToPathConverter class, to do this you need to add in ConvertParamater and the string value.

Heres an example : –

 <Image Grid.Row=”0″ Grid.Column=”1″ Margin=”20″ HorizontalAlignment=”Left” Source=”{Binding SelectedObject.Room, Converter={StaticResource NameToPathConverter}, ConverterParameter=room}” />

So you can see here that I am saving the value room into ConvertParameter which gets transferred to the NameToPathConverter class.

See here in the NameToPathConverter class on how it is implemented: –

public class NameToPathConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string name = value as string;

        switch (parameter.ToString())
        {
            case “background”:
                GetPath(value, name, “background”);
                break;
            case “room”:
                GetPath(value, name, “room”);
                break;
            default:
                GetPath(value, name, “other”);
                break;
        }
        return GetPath(value, name, “other”);
}

So you can see that you can call the write methods, useful for setting a correct path variable in the settings based on the variable name passed in initially from the ConverterParameter string value passing. Useful eh??

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: