MVVM utilizing RadWindow for your popups

1 posts, 0 answers
  1. k f
    k f avatar
    109 posts
    Member since:
    Jun 2006

    Posted 03 Feb 2011 Link to this post

    Requirements

    RadControls version Q3 2010

     

    .NET version

     

    Visual Studio version 2010

     

    programming language C#

     

    browser support

    all browsers supported by RadControls


    PROJECT DESCRIPTION

    Here is some code for a RadWindowController I came up and use in my LOB applications.  Keep in mind your needs will vary and feel free to modify this code as needed.

    // 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
      
    }

    For a detailed scenario of how I use this a link to a fully working sample, you can find a write up here.

    http://www.dotnetpatterns.net/content/203-Using-RadWindow-for-Popups-in-your-MVVM-application
Back to Top