internetmw
internetmw

Reputation: 699

WPF Binding Formatexception

In a C# Wpf application I have an XML Binded datasource.

I want to update some xml like this:

loop.Innerxml = "false"

This value is binded (as a boolean) to a control. However when doing this, a formatexception comes up saying that a string is not a valid boolean (logical). However if the false is not entered as a string, I can't update the innerxml...

Any advice?

Upvotes: 0

Views: 635

Answers (1)

robertos
robertos

Reputation: 1796

You can use a Converter to convert your Strings to Booleans when the binding happens.

For more about converters, see http://www.scip.be/index.php?Page=ArticlesNET22&Lang=EN.

Code sample:

[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)
  {
    throw new NotSupportedException();
  }
}

Upvotes: 1

Related Questions