WPF Ivalueconverter empty values
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(type开发者_运维问答of(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}}"
I'm not sure, but if you use "XPath=myelement", the string should be
myCheckBox.DataContext="<myelement>true</myelement>";
精彩评论