tags:

views:

29

answers:

1

Hi,

I use a Ivalueconverter to convert a string to a boolean using an xml datasource. This works fine until I manually change the xml like so:

myelement.InnerXml = "true"

I then receive a formatexception saying the string is not a valid boolean, I check the value that goes in to my converter and it is equal to ""

Here is my converter:

[ValueConversion(typeof(string), typeof(bool))]
public class StringToBoolConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        return TypeDescriptor.GetConverter(typeof(bool)).ConvertFrom(value); }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        return value.ToString();
    }
}

I bind the converter like so: <local:StringToBoolConverter x:Key="stringbool"></local:StringToBoolConverter>

And apply it in the binding: IsChecked="{Binding Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, XPath=myelement, Converter={StaticResource stringbool}}"

+1  A: 

I'm not sure, but if you use "XPath=myelement", the string should be

myCheckBox.DataContext="<myelement>true</myelement>";
vorrtex
I don't know, the binding itself is ok, it's just that it seems like the manual editing of the xml does not transport to the Ivalueconverter...
internetmw