Initially I used a method I found in Prism to build a popup controller I use in my applications. However, in some cases I wanted a header or possibly allow the popup to be resized etc, etc so I decided to come up with a way to do this with the RadWindow.
FYI, a huge thanks to Telerik support for suppying a sample that allows the Popup to adjust as the browser size adjusts.
For now, instead of creating a project, I'm just going to post the code in this article.
If you look through my Silverlight sections you see most of the apps I build, I use Prism because I like the Module concept. So with that, personally I create this class in my Common assembly.
To start, create a class called RadWindowController. Note you can all it whatever you like.
Next, paste this code in.
Code:
// Code written by Kris Frost http://www.n-stech.com publicinterfaceIRadWindowController { void CloseRadWindow(); void ShowRadWindow(UserControl content, string headerText); void ShowRadWindow(UserControl content, bool isRestricted, ResizeMode resizeMode, Telerik.Windows.Controls.WindowStartupLocation windowStartUpLocation, string headerText); eventEventHandler MainRadWindowClosed; } [Export(typeof(IRadWindowController))] publicclassRadWindowController : IRadWindowController { RadWindow _radWindow; // Used so we can alert any consumer when the MainRadWindow has closed. publiceventEventHandler MainRadWindowClosed; public RadWindowController() { _radWindow = newRadWindow(); _radWindow.Closed += newEventHandler<WindowClosedEventArgs>(RadWindow_Closed); SetDefaultWindowValues(); } // Call if you need to add your own command to close a popup window publicvoid CloseRadWindow() { _radWindow.Close(); } public void ShowRadWindow(UserControl content, string headerText) { // There should only ever be one rad window open using this controller // The controller is set to Modal so a user should not be able to open // More than these at once anyway. If other options are needed. Create a new RadWindow in code. if (_radWindow.IsOpen) _radWindow.Close(); // We have to wire up to the Current Host's content here so the Rad Window stays centered when the browswer is resized Application.Current.Host.Content.Resized += newEventHandler(Content_Resized); _radWindow.Content = content; _radWindow.Header = headerText; _radWindow.ShowDialog(); } // Shows the view passed in a popup window publicvoid ShowRadWindow(UserControl content, bool isRestricted, ResizeMode resizeMode, Telerik.Windows.Controls.WindowStartupLocation windowStartUpLocation, string headerText) { ShowRadWindow(content, headerText); } #region Private Methods void Content_Resized(object sender, EventArgs e) { _radWindow.Left = (Application.Current.RootVisual.RenderSize.Width / 2 - (_radWindow.ActualWidth / 2)); _radWindow.Top = Application.Current.RootVisual.RenderSize.Height / 2 - _radWindow.ActualHeight / 2; } void RadWindow_Closed(object sender, WindowClosedEventArgs e) {// Set our window back to default values SetDefaultWindowValues(); // Unregister when the Window is closed. Application.Current.Host.Content.Resized -= newEventHandler(Content_Resized); // Remove whatever is assigned to the content when the window is closed. _radWindow.Content = null; if(MainRadWindowClosed != null) MainRadWindowClosed(this, null); } void SetDefaultWindowValues() { // Set Default window values to use if a caller doesn't need custom _radWindow.IsRestricted = false; _radWindow.ResizeMode = ResizeMode.NoResize; _radWindow.WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterOwner; } #endregion}
Most of this code is self explanatory. If you have any questions, Telerik has really nice documentation which you can find here.
To cover the hightlights, I created an interface which will contain the Methods RadWindowController will implement. There is CloseRadWindow() and as the name implies it's just a call to close the RadWindow. Note, you can do this inside the UserControl you pass to one of the ShowRadWindow() methods as well.
You can take a look here for one way of doing this.
I have 2 ShowRadWindow methods. The 2 parameter method, takes a UserControl that wiill be assigned to the RadWindow's Content property.
Then you have and a string which will be assigned to the RadWindows Header.
We have a SetDefaultWindowValues() method use to set our standard settings. Do anything you like here.
The second ShowRadWindow() is if we need the ability to change the display properties of our window. Again these should be fairly intuitive and if not, please see the Telerik documentation.
The most important things of note are:
1)
// We have to wire up to the Current Host's content here so the Rad Window stays centered when the browswer is resized
Application.Current.Host.Content.Resized += newEventHandler(Content_Resized);
Along with:
void Content_Resized(object sender, EventArgs e)
{
Again, thanks to Telerik support for supplying this. Here we're subscribing to the Host widows Content_Resized event so we can readjust the position of the RadWindow if the user readjusts the size of the browser.{
_radWindow.Left = (
Application.Current.RootVisual.RenderSize.Width / 2 - (_radWindow.ActualWidth / 2));
_radWindow.Top = Application.Current.RootVisual.RenderSize.Height / 2 - _radWindow.ActualHeight / 2;
}_radWindow.Top = Application.Current.RootVisual.RenderSize.Height / 2 - _radWindow.ActualHeight / 2;
2) In some cases we need to perform actions when a user closes a window. So in the IRadWindowController interface, I created the MainRadWindowClosed event. This allows any caller to subscribe and be notified when the RadWindow closes.
In private code, I reset the default window values, and clear the Content and unsubscribe from the Hosts Content_Resized event.
3) This class is made available to my modules via MEF. You'll note the [Export] attribute for the class. With this, all I have to do is use MEF's Import attribute and now I have a popupcontroller for use anywhere in my application.
That's pretty much all there is too it. Now I know this isn't going to suit everyones needs but it should good start for anybody to change it to whatever they need.
Since I'm not posting a fully working sample with this, I'll cover how I go about using the RadWindowController.
As I mentioned I build most of my apps using Prism. For each module I build I create a Controller.
The controller takes care of loading the module's Views into regions and any other initialization I need to do when the module loads.
Also, I use a controller when I need to communicate between view models. Also, I use the controller when I need to load popups. So instead of coupling views into my ViewModels and writing code in the ViewModels to load these views into popup windows, I will create a Controller method for my ViewModels to call. The controller takes care of opening and closing the popups. You can check out the code in this lesson. The Silverlight_MVVM_Template_Northwind solution contains an implemenation of this RadWindowController.
You can open the Northwind.Modules.Orders project and look at the OrderController.
You will find I have an Import for the RadWindowController
Code:
[Import] publicIRadWindowController RadWindowController { get; set; }
Code:
public void CloseMapOrderView(){RadWindowController.CloseRadWindow();} publicvoid ShowMapOrderView(SmartObservableCollection<OrderModel> orders) {if (orders == null || orders.Count < 1) MessageBox.Show("ShowMapOrderView must contain orders."); else { _mapOrderView.ViewModel.Initialize(orders); ; RadWindowController.ShowRadWindow(_mapOrderView, "Map Orders"); RadWindowController.MainRadWindowClosed += newEventHandler(OrderController_MainRadWindowClosed); }}
We then pass the View to the RadWindowController.ShowRadWindow() and then we subscribe to the RadWindowController MainRadWindowClosed event because we want to call cleanup on the MapOrderViewModel bound to the MapOrderView.
That ends this lesson. Again, this wasn't created to solve all scenarios but it's a good start for most I run into and I will adjust when the need arises.
Please feel free to use this code any way you like. All I ask is you keep my name and companies URL with the code.
Thanks and enjoy.
Kris







Section Widget
Categories Widget (Show All)


vBulletin Message