Infosys Microsoft Alliance and Solutions blog

« Why is .NET Micro Framework so crucial for Autonomous digital enterprise | Main | WPF Data Binding »

WPF - Passing string to ConverterParameter

There are enough online sources that talk about how to buid and use custom Converters in WPF and also how to pass parameters to these converters. However for some reason, all these examples tend to either use a single integer or a single word string. So recently when I had a need for passing a sentence as parameter, I was confused.

Fortunately the simple trick of using single quotes inside of double quotes to provide strings worked in this case as well. Following are two ways you can pass a string that has multiple words to a converter.

        <TextBlock Text="{Binding Source={x:Static sys:DateTime.Now}, Converter={StaticResource custConverter}, ConverterParameter='The time of the day is '}" />

Though VS color coding goes for a toss, the designer still loads fine and code also runs fine. If you use the more elaborate syntax for setting the binding, the regular double quotes will also work.



                <Binding Source="{x:Static sys:DateTime.Now}" Converter="{StaticResource custConverter}" ConverterParameter="The time of the day is " />



And the converter logic in this case is pretty trivial and is as below.

    public class DateConverter : IValueConverter


        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)


            DateTime dt = (DateTime)value;

            return parameter.ToString() + dt.ToLongDateString();



        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)


            throw new NotImplementedException();




BTW, needless to say that you need to include the appropriate namespace for the DateTime.Now to work and that will be xmlns:sys="clr-namespace:System;assembly=mscorlib"

[Updated: April 25, 2008] There is another simple way to pass strings. First define a resource as below

        <sys:String x:Key="mystring">The time of the day is </sys:String>

And now use this resource for setting the value of ConverterParameter as below

        <TextBlock Text="{Binding Source={x:Static sys:DateTime.Now}, Converter={StaticResource custConverter}, ConverterParameter={StaticResource mystring}}" />


Hi, is there a way to pass an entry from a resource dictionary as parameter to a converterparamater?

Klaus, yes. Check this forum entry where I have an example of the same -

The converter parameter is set using StaticResource syntax and you can point this to your specific value

Thank you so much! I was trying in vain to pass a format string through. Works when I do this:

But not when I do any variation of:<<<<<

So I used your method as a workaround. In fact, it's nice because now I've got that format parameter in only one place in the code instead of everywhere.

You saved me much time. Best way to pass string parameters.

Great stuff Atul! Hey can we data bind the converter parameter? It will be great if you could let me know!

@Ananth, I am not sure i understand your question. Converter and converter parameter are used in binding expressions. So the intent is to modify the binding value before the binding happens and then bind with the converted value.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.

Subscribe to this blog's feed

Follow us on

Blogger Profiles

Infosys on Twitter