views:

589

answers:

3

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.

Thanks in advance.

A: 

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.

HTH,
Kent

Kent Boogaart
Article doesn't really help in programmatically detecting that a Binding is failing.
AnthonyWJones
@Anthony: it just takes imagination. A trace listener that logs the problem / raises an exception will do it.
Kent Boogaart
@Kent Boogaart: problem is, there's no equivalent of TraceListener in Silverlight :-(
Duncan Bayne
A: 

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.

AnthonyWJones
Anthony, that is a very creative solution to this problem. I will probably go a different route by changing my whole approach to implementing "AutoBinding" on custom, composite controls. I really appreciate the effort. Thanks
Jersey Dude
+1  A: 

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.

JustinAngel