private static ValidationBindingContext GetBindingForProperty(Type boundType, string boundPropertyName, FrameworkElement root) { foreach (FrameworkElement element in LogicalTreeHelper.GetChildren(root).OfType()) { FieldInfo[] properties = element.GetType().GetFields(BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Static | BindingFlags.FlattenHierarchy); foreach (FieldInfo field in properties) { if (field.FieldType == typeof(DependencyProperty)) { DependencyProperty dp = (DependencyProperty)field.GetValue(null); if (BindingOperations.IsDataBound(element, dp)) { BindingExpression bindingExpression = BindingOperations.GetBindingExpression(element, dp); if (boundType == bindingExpression.DataItem.GetType()) { if (boundPropertyName == bindingExpression.ParentBinding.Path.Path) { return new ValidationBindingContext(bindingExpression, element as UIElement); } } } } } // Not found so check all child elements ValidationBindingContext bindingContext = GetBindingForProperty(boundType, boundPropertyName, element); if (bindingContext != null) return bindingContext; } return null; } internal class ValidationBindingContext { public BindingExpression Expression { get; private set; } public UIElement Element { get; private set; } public ValidationBindingContext(BindingExpression expression, UIElement element) { Element = element; Expression = expression; } }
public void Test_MainWindow_Databindings2ndTry() { TestUtilsComputerPart.GetTestFactory().CreateManySavedBusinessObject(10); //AvalonTestRunner.RunDataBindingTests(new ComputerPartDetail()); var partWindow = new MainWindow(); partWindow.Show(); try { //var partWindow = Window.GetWindow(new ComputerPartDetail()); var desc = TestBindingForProperty(typeof(ComputerPart), "Description", partWindow); Assert.IsNotNull(desc); var price = TestBindingForProperty(typeof(ComputerPart), "Price", partWindow); Assert.IsNotNull(price); var stock = TestBindingForProperty(typeof(ComputerPart), "Stock", partWindow); Assert.IsNotNull(stock); var cost = TestBindingForProperty(typeof(ComputerPart), "Cost", partWindow); Assert.IsNotNull(cost); } finally { partWindow.Close(); } }
This website is best viewed in Internet Explorer 7 & 8; Firefox 3.6.11; Opera 10.63; & Safari 4.