locked
how to detect a click outside a Usercontrol in windows store app with c#? RRS feed

  • Question

  • hi , i need to close the user control if i click outside the user control so plz give me event for handle this task  and i try the LostFocus but that is not working.
    Saturday, April 12, 2014 7:04 AM

Answers

  • Hi Megha,

    Try with following code, let's say if you have a XAML user control named "usercontrol", I attach a tap(click) event to the whole page, when the mouse click somewhere, the pageRoot_Tapped event fires, by using the PointerEnter/PointerExist I know if the mouse action is inside the usercontrol or not:

            public MainPage()
            {
                this.InitializeComponent();
                this.AddHandler(UIElement.TappedEvent, new TappedEventHandler(pageRoot_Tapped), true);   
            }
    
            private bool ismousein = false;
            private void usercontrol_PointerEntered(object sender, PointerRoutedEventArgs e)
            {
                ismousein = true;
            }
            private void usercontrol_PointerExited(object sender, PointerRoutedEventArgs e)
            {
                ismousein = false;
            }
            private void pageRoot_Tapped(object sender, TappedRoutedEventArgs e)
            {
                if(!ismousein)
                {
                    //implement something here
                }
            }
    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Monday, April 14, 2014 6:56 AM
    Moderator