How does one detect SetBinding success or failure in Silverlight?

跟風遠走 提交于 2020-01-03 17:23:46

问题


Simple binding from C#:

  Binding binding = new Binding(SourceName);
  binding.Mode = BindingMode.TwoWay;
  BindingExpressionBase beb = SetBinding(SourceDependencyProperty, binding);

I would like to detect whether or not the SetBinding was successful. SetBinding obviously knows when it has an issue because it displays in the Output window tracing when the application is running:

System.Windows.Data Error: BindingExpression path error: 'InterestRate' property not found on 'Tc.Views.TestAccount' ...

The BindingExpressionBase looks the same to me whether SetBinding() succeeds or fails and there is no exception thrown. I tried different values for the binding notification flags as well.


回答1:


I suggest you use Karl Shiflett's Glimpse for Silverlight. The GlimpseService exposes an API that'll allow you to handle any binding exceptions manually.

The basic technique is fairly simple - listen to Application.UnhandledException and Application.RootVisual.BindingValidationError and you should be able to intercept binding errors.




回答2:


A really tough one this. I had to think about it but I don't you are going to like the answer (no its not 42).

The strict answer is no there isn't. However there is a horrible one-shot solution which frankly I don't recommend but if its absolutely unavoidable might be useful. First you need a value converter:-

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

Now you can modify your source code as follows:-

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.
}

This code assumes that an appropriate DataContext is already in place. The Converter only handles the typical conversions between the basic system types that implement IConvertible (String, Int, Double, DateTime etc). It works because Convert will only get called if the property path is found.




回答3:


You might want to check this out. It's specific to WPF, but should be mostly relevant to Silverlight too, and might give you some ideas about how to go about trapping these issues.



来源:https://stackoverflow.com/questions/1693378/how-does-one-detect-setbinding-success-or-failure-in-silverlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!