XAML: How to use a ConverterParameter which is not a string in XAML?

Imagine the following case: I have a TextBox and a TextBlock and a boolean property IsEditing.

If IsEditing is true, the TextBox is visible and the TextBlock is hidden and reversely.

In this case I could use a ViewModel with two properties of type Visibility and the problem would be solved.

However in this post, I want to use with Converters.

The problem is the fact that I have only one boolean property and that Visibility controls property are both bound to it with different behaviors.

So I will use a ConverterParameter to differentiate them.

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    return ((bool)value ^ (parameter != null && (bool)parameter)) ? Visibility.Visible : Visibility.Collapsed;
}

The problem in my binding is the fact that I can’t write this:

Visibility="{Binding IsEditing, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=True}"

Indeed, with it, the parameter is a string and not a boolean as expected.

Of course I can make the binding to solve this by code but it isn’t the goal of this post.

So how to do it in xaml?

Like this:

<TextBlock.Visibility>
    <Binding Path="IsEditing"
            Converter="{StaticResource BooleanToVisibilityConverter}">
        <Binding.ConverterParameter>
            <system:Boolean>True</system:Boolean>
        </Binding.ConverterParameter>
    </Binding>
</TextBlock.Visibility>

Hope that helps

Published Thu, Aug 26 2010 10:27 by Matthieu MEZIL
Filed under: , ,

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: