locked
Close Event for the Usercontrol when the button is clicked RRS feed

  • Question

  • Hi All,

    Could you please help me by providing some code to the particular scenario

    I have a UserControl 1 which acts like a popup in other usercontrol2,There is a button and some action to it in usercontrol1,including with that action it has to even close the usercontrol1 in the same button where the command is binded to it.

    Thanks in Advance

    Sunday, November 3, 2013 5:28 PM

Answers

  • Do the UserControl in the Popup and the Popup itself have the same DataContext/view model? Then you should bind the IsOpen property of the Popup to a Boolean property of this view model and set this to false when the command is executed. The view model also needs to implement the INotifyPropertyChanged interface and raise its ProperyChanged event then this Boolean property is set.

    It's hard for us to give you any more advice if you don't post the relevant code and markup.

    Monday, November 4, 2013 9:18 AM

All replies

  • Is it hosted in a popup?  For us to help we need to know what it really is not what it is like.

    How is the usercontrol created?  Is it in code or is it in XAML.  If it is in code and it is hosted in a popup then you can create a constructor in which you pass a reference to the popup.  Then in the code executed in the button click you can set the popup IsOpen to false to close the popup.  


    Lloyd Sheen

    Sunday, November 3, 2013 9:22 PM
  • User controls don't have a close method as they are not considered a window, rather they are controls hosted in another window-like container.  Popups don't have close events either, they just are isOpen = true, or isOpen = false.

    The trick is to change the visibility on objects that are not windows or don't have a close method.


    JP Cowboy Coders Unite!

    Monday, November 4, 2013 1:52 AM
  • the usercontrol created in the xaml,and used as popup in the parent usercontrol code,need to close the usercontrol when the button is clicked.
    Monday, November 4, 2013 3:42 AM
  • Hi,

    You can set the visibility of the user control to hidden if you want to preserve the instance of the user control. If you want to destroy the user control after been used, you should put the user control inside of a container and to close it just clear the content of the container.

    Example:

    <Grid x:Name="container">

         <usercontrol1/>

    </Grid>

    In the code behind:

    To close just do: container.Children.Clear();

    Monday, November 4, 2013 4:39 AM
  • But there is already ICommand is binded to that control which is in usercontrol including this even the usercontrol need to be closed

    Have tried with event triggers but could not get the expected result.

    Monday, November 4, 2013 6:07 AM
  • Do the UserControl in the Popup and the Popup itself have the same DataContext/view model? Then you should bind the IsOpen property of the Popup to a Boolean property of this view model and set this to false when the command is executed. The view model also needs to implement the INotifyPropertyChanged interface and raise its ProperyChanged event then this Boolean property is set.

    It's hard for us to give you any more advice if you don't post the relevant code and markup.

    Monday, November 4, 2013 9:18 AM
  • You need to post the relevant part of the code here. At least I can't understand what is really your problem.
    Tuesday, November 5, 2013 2:44 PM