Silverlight UserControl
I have 2 user controls. My MainPage control and my edit functions control. My edit control uses WCF to update a database. Once this is done I need to reload the Mainpage control which searched the database to see the changes mad by the edit functions control.
I did this in an asp.net project by redirecting to the MainPage - do you know 开发者_如何学运维how this is done in Silverlight.
Ok so i did this by creating a Button at the top of my main page which fires a reload of the page. I then used the Visual tree helper to get the button and invoke it. I did this by:
Create a class to search Controls: public static class ControlFinder { public static T FindParent(UIElement control) where T : UIElement { UIElement p = VisualTreeHelper.GetParent(control) as UIElement; if (p != null) { if (p is T) return p as T; else return ControlFinder.FindParent(p); } return null; }
}
Find the Button within the controls:
UserControl uc = ControlFinder.FindParent(this); UserControl mainControls = ControlFinder.FindParent(uc); var PageGrid = VisualTreeHelper.GetChild(mainControls, 0); var StackPanel = VisualTreeHelper.GetChild(PageGrid, 0); Button RefreshButton = (Button)VisualTreeHelper.GetChild(StackPanel, 0);
Invoke the button event which fires the refresh ButtonAutomationPeer buttonAutoPeer = new ButtonAutomationPeer(RefreshButton); IInvokeProvider invokeProvider = buttonAutoPeer.GetPattern(PatternInterface.Invoke) as IInvokeProvider; invokeProvider.Invoke();
精彩评论