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();
}
}
精彩评论