This is a migrated thread and some comments may be shown as answers.

How to close RadWindow from itself

2 Answers 98 Views
Window
This is a migrated thread and some comments may be shown as answers.
Carlos
Top achievements
Rank 2
Carlos asked on 01 Aug 2011, 07:28 PM
How can I close a RadWindow that contains a userControl from itself (usercontrol code)... and using a ViewModel?

Thanks

2 Answers, 1 is accepted

Sort by
0
Konstantina
Telerik team
answered on 03 Aug 2011, 07:41 AM
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 >>
0
hwsoderlund
Top achievements
Rank 1
answered on 03 Aug 2011, 08:46 AM
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;
            }
        }
    }
Tags
Window
Asked by
Carlos
Top achievements
Rank 2
Answers by
Konstantina
Telerik team
hwsoderlund
Top achievements
Rank 1
Share this question
or