tags:

views:

27

answers:

1

Hi,

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?

A: 

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();
  }
}
robertos
Hi Thanks! Allthough it seems that when calling Loop.innerxml = "false", a formatexception rises saying that value == "" and is thus not a valid boolean...
internetmw
Is Innerxml a valid DependencyProperty or does its setter raise the PropertyChanged event (from INotifyPropertyChanged)? If not, changing the value will not notify the binding system to update the value.
robertos
I think it simply raises the propertychanged event... How can I change this?
internetmw