public static void RefreshSelectedObject(DependencyObject editor) { foreach (PropertyGrid grid in editor.GetChildren<PropertyGrid>()) { grid.RefreshSelectedObject(); } }
/// <summary> /// Recursively force each bound property to refresh it's binding /// </summary> /// <param name="dependencyObject">UI control</param> /// <remarks>This "walks" the visual tree to find controls that have a bound property /// and it then refreshes the binding, which forces any validation rule to be re-run. /// If the control does not have a bound property, the children controls are checked. /// This handles situations when text boxes or other validated controls are within /// an <c>ItemsControl</c>, <c>ListBox</c>, <c>DataGrid</c>, <c>Grid</c> /// <c>StackPanel</c>, or <c>Canvas</c>. Child controls for <c>Button</c>, /// <c>HyperlinkButton</c>, and <c>ValidationSummary</c> are ignored, since they shouldn't /// have bound children with validation rules.</remarks> private static void RefreshBinding(DependencyObject dependencyObject) { Debug.WriteLine(dependencyObject.GetType()); // stop if we've reached a validation summary var validationSummary = dependencyObject as ValidationSummary; if (validationSummary != null) return; // don't do buttons - should be nothing to validate var button = dependencyObject as Button; if (button != null) return; // don't do hyperlink buttons - should be nothing to validate var hyperLinkButton = dependencyObject as HyperlinkButton; if (hyperLinkButton != null) return; foreach (var item in dependencyObject.GetChildren()) { var found = false; // get bound property (use list from BindingHelper, so we don't repeat it in this class) DependencyProperty boundProperty; if (BoundProperties.TryGetValue(item.GetType(), out boundProperty)) { // get BindingExpression and, if exists, force it to refresh var be = ((FrameworkElement)item).GetBindingExpression(boundProperty); if (be != null) be.UpdateSource(); // binding refreshed, so don't look for children found = true; Debug.WriteLine(string.Format("{0} binding refreshed ({1}).", item, item.GetValue(boundProperty))); } // get children recursively if bound property has not already been found if (!found) { RefreshBinding(item); } } }