Disclaimer: Don’t do it. Whatever brought you here, chances are you’re doing something wrong, because you’re most likely about to do something very gnarly that you shouldn’t be doing with your view and a whole pile of code-behind. You could make your viewmodel more intelligent, you could implement IDataErrorInfo or otherwise
That being said, here’s how it’s done.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public static Dictionary<DependencyProperty, BindingBase> GetAllBindings(DependencyObject d) { if (d == null) { throw new ArgumentNullException("d"); } var bindings = new Dictionary<DependencyProperty, BindingBase>(); var lve = d.GetLocalValueEnumerator(); while (lve.MoveNext()) { DependencyProperty dp = lve.Current.Property; var expr = BindingOperations.GetBindingBase(d, dp); if (expr != null) { bindings.Add(dp, expr); } } return bindings; } |
If you are doing (or are thinking about doing) anything fancy with reflection to determine all properties and values on an object, you may want to consider using DependencyObjects instead. You get everything that DependencyObjects give you for free, and you can do everything without resorting to slow (and often obfuscating) reflection. —DKT