private static void targetElement_Loaded(object sender, RoutedEventArgs e) { try { FrameworkElement targetElement = sender as FrameworkElement; // release handler to prevent memory leaks targetElement.Loaded -= new RoutedEventHandler(targetElement_Loaded); RelativeSourceBase bindings = GetBinding(targetElement); if (bindings is RelativeSourceBinding) { // get the binding configuration RelativeSourceBinding bindingConfiguration = bindings as RelativeSourceBinding; ProcessBinding(targetElement, bindingConfiguration); } else if (bindings is BindingList) { // get the binding configuration BindingList list = bindings as BindingList; foreach (RelativeSourceBinding bindingConfiguration in list) { ProcessBinding(targetElement, bindingConfiguration); } } } catch (Exception ex) { // ignore this exception, because the SL binding engine does not throw exceptions when a binding is wrong. } }
public static void SetBinding(DependencyObject obj, RelativeSourceBase value) { obj.SetValue(BindingProperty, value); }