我使用Ivalueconverter将字符串转换为使用xml数据源的布尔值。这很好用,直到我像这样手动更改xml:
myelement.InnerXml = "true"
然后我收到一个格式异常,指出字符串不是有效的布尔值,我检查进入转换器的值,它等于"“
下面是我的转换器:
[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) {
return value.ToString();
}
}我像这样绑定转换器:<local:StringToBoolConverter x:Key="stringbool"></local:StringToBoolConverter>
并将其应用到绑定中:IsChecked="{Binding Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, XPath=myelement, Converter={StaticResource stringbool}}"
发布于 2010-11-01 02:12:42
我不确定,但是如果你使用"XPath=myelement",字符串应该是
myCheckBox.DataContext="<myelement>true</myelement>";https://stackoverflow.com/questions/4063657
复制相似问题