rockshire
rockshire

Reputation: 61

Binding to a property of a custom converter

I've written a converter BoolToStringConverter. The converter has two properties TrueString and FalseString. Here's how I've used it in XAML

<UserControl.Resources>
    <local:BooleanToStringConverter x:Key="BooleanToStringConverter" TrueString="{Binding Strings.Open, Source={StaticResource MyStrings}}"></local:BooleanToStringConverter>
</UserControl.Resources>

This compiles ok, but I get an xml parse exception when running it. If I change the setting of the TrueString property to TrueString = "Open" it all works fine.

Here's the converter being used:

<Button x:Name="MyButton" Content="{Binding Path=IsOpen, Converter={StaticResource BooleanToStringConverter}}" Command="{Binding MyCommand}" VerticalAlignment="Top" Style="{StaticResource MyStyle}" Margin="0,2,10,2"/>

Any ideas what is wrong? All I want to do as set a property of a local resource to a localized value.

EDIT Here's my converter class

public class BooleanToStringConverter : IValueConverter
{
    public BooleanToStringConverter()
    {
    }

    public string TrueString
    {
        get;
        set;
    }

    public string FalseString
    {
        get;
        set;
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool boolValue = System.Convert.ToBoolean(value, CultureInfo.InvariantCulture);

        return boolValue ? TrueString : FalseString;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }     
}

Here's the runtime exception message:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.dll

Additional information: Set property 'Optimize.Client.Presentation.BooleanToStringConverter.FalseString' threw an exception. [Line: 18 Position: 86]

Upvotes: 2

Views: 2278

Answers (2)

JBrooks
JBrooks

Reputation: 10013

You can also set the public properties in your XAML like this:

<localHelpers:BoolToTextConverter x:Key="boolToTextConverter">
    <localHelpers:BoolToTextConverter.TrueText>
        Sent
    </localHelpers:BoolToTextConverter.TrueText>
    <localHelpers:BoolToTextConverter.FalseText>
        Not Sent
    </localHelpers:BoolToTextConverter.FalseText>
</localHelpers:BoolToTextConverter>

The full example is on my blog post here.

Upvotes: 1

Shawn Kendrot
Shawn Kendrot

Reputation: 12465

You cannot bind to the TrueString and FalseString properties. From the MSDN help:

in order to be the target of a binding, the property must be a dependency propert

You can try using the ConverterParameter part of the binding for your xaml

<Button x:Name="MyButton" Content="{Binding Path=IsOpen, Converter={StaticResource BooleanToStringConverter}, ConverterParameter=Open}" 
        Command="{Binding MyCommand}" VerticalAlignment="Top" 
        Style="{StaticResource MyStyle}" Margin="0,2,10,2"/>

You could also make your converter less generic and only handle Open/Closed strings.

Another option is to have your value converter extend DependencyObject, and convert your properties to DependencyProperties.

Upvotes: 1

Related Questions