How to close RadWindow from itself

3 posts, 0 answers
  1. Carlos
    Carlos avatar
    50 posts
    Member since:
    Feb 2011

    Posted 01 Aug 2011 Link to this post

    How can I close a RadWindow that contains a userControl from itself (usercontrol code)... and using a ViewModel?

    Thanks
  2. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 03 Aug 2011 Link to this post

    Hello Carlos,

    Initially, you could use the Close() method to close the RadWindow. Could you please give us some more information of what exactly you are trying to achieve? Providing some more details about your case will be very helpful for suggesting you the best approach.

    Looking forward to your reply.

    Greetings,
    Konstantina
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
  3. DevCraft banner
  4. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 03 Aug 2011 Link to this post

    If you are just looking to close the window from a button I would just use a simple behaviour on the button. Below is the behaviour I use for that purpose. It has a couple of other properties that you do not need if all you want to do is just close the window. However, if you want to initiate the closing of the window from your viewmodel, it is a little trickier. I use MVVM Light, so I would use the Messenger functionality in that framework to send a message from the viewmodel to the view. The view would then close the parent window using code similar to the method "CloseParentWindow" in the behaviour below.

    /// <summary>
        /// When the button is clicked, its parent RadWindow will be closed
        /// </summary>
        public class CloseParentWindowOnClick : Behavior<Button>
        {
            private bool _DialogResultValue;
     
            /// <summary>
            /// DialogResultValue Property
            /// </summary>
            public bool DialogResultValue
            {
                get { return _DialogResultValue; }
                set { _DialogResultValue = value; }
            }
     
     
     
            protected override void OnAttached()
            {
                this.AssociatedObject.Click += new RoutedEventHandler(AssociatedObject_Click);
     
                base.OnAttached();
            }
     
            protected override void OnDetaching()
            {
                this.AssociatedObject.Click -= AssociatedObject_Click;
     
                base.OnDetaching();
            }
     
            void AssociatedObject_Click(object sender, RoutedEventArgs e)
            {
                CloseParentWindow();
            }
     
            public void CloseParentWindow()
            {
                var win = AssociatedObject.ParentOfType<RadWindow>();
     
                if (win != null)
                {
                    win.DialogResult = DialogResultValue;
     
                    win.Close();
                    win = null;
                }
            }
        }
Back to Top