这真的很难。我不得不考虑这个问题,但我不会喜欢答案(不是它不是42)。
严格的答案是没有没有。然而,坦率地说,我不推荐使用可怕的一次性解决方案,但如果它绝对不可避免的话可能会有用。首先你需要一个值转换器: -
public class ConvertibleValueConverter : IValueConverter
{
public bool Converted { get; private set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Converted = true;
return ((IConvertible)value).ToType(targetType, culture);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((IConvertible)value).ToType(targetType, culture); ;
}
}
现在你可以按如下修改源代码: -
Binding binding = new Binding(SourceName);
binding.Mode = BindingMode.TwoWay;
binding.Converter = new ConvertibleValueConverter();
BindingExpressionBase beb = SetBinding(SourceDependencyProperty, binding);
if (!((ConvertibleValueConverter)binding.Converter).Converted)
{
// Path SourceName was not found.
}
此代码假定一个合适的DataContext已经到位。 Converter仅处理实现IConvertible(String,Int,Double,DateTime等)的基本系统类型之间的典型转换。它的工作原理是因为Convert
只有在找到属性路径时才会被调用。
+1优秀的问题。可悲的是,我认为这可能是微软说的话“哦,我没有想到” – AnthonyWJones 2009-11-07 17:03:31