开发者

WPF Binding Formatexception

开发者 https://www.devze.com 2023-01-22 23:58 出处:网络
In a C# Wpf application I have an XML Binded datasource. I want to update some xml like this: loop.Innerxml = \"false\"

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 advi开发者_StackOverflow中文版ce?


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();
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消