locked
user control closed event RRS feed

  • Question

  • which user control event should I hook so that the user control can perform shutdown processing before the Closed event of the Window object?

    I tried Dispatcher.ShutdownStarted. But that fires after the Closed event of the Window that contains the user control.

    thanks,

    <Window x:Class="wpfDemo.steve4"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:wpfDemo"
            mc:Ignorable="d"
            Title="steve4" Height="300" Width="300">
        <Grid>
        <local:control4/>
      </Grid>
    </Window>
    
      public partial class steve4 : Window
      {
        public steve4()
        {
          InitializeComponent();
          this.Closed += Steve4_Closed;
          this.Closing += Steve4_Closing;
        }
    
        private void Steve4_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
          Debug.Print("window Steve4. Closing.");
        }
    
        private void Steve4_Closed(object sender, EventArgs e)
        {
          Debug.Print("window Steve4. Closed.");
        }
      }
    
    
    <UserControl x:Class="wpfDemo.control4"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:wpfDemo"
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <Grid>
        <TextBox x:Name="TextBox1" Width="200" Padding="5" 
                 VerticalAlignment="Top" HorizontalAlignment="Left"></TextBox>
        </Grid>
    </UserControl>
    
      public partial class control4 : UserControl
      {
        public control4()
        {
          InitializeComponent();
          Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
        }
    
        private void Dispatcher_ShutdownStarted(object sender, EventArgs e)
        {
          Debug.Print("control4 shutdown. " + TextBox1.Text);
        }
      }
    
    

    Friday, June 17, 2016 7:07 PM

Answers

  • Hi Steve,

    You could get the root window of this user control and register the Closing event of root window.

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        Window currentWindow = Window.GetWindow(this);
        currentWindow.Closing += currentWindow_Closing;
    }
    
    void currentWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
    
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Andy ONeill Saturday, June 18, 2016 8:22 AM
    • Marked as answer by Steve Richter Saturday, June 18, 2016 12:13 PM
    Saturday, June 18, 2016 3:26 AM

All replies

  • Hi Steve,

    You could get the root window of this user control and register the Closing event of root window.

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        Window currentWindow = Window.GetWindow(this);
        currentWindow.Closing += currentWindow_Closing;
    }
    
    void currentWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
    
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Andy ONeill Saturday, June 18, 2016 8:22 AM
    • Marked as answer by Steve Richter Saturday, June 18, 2016 12:13 PM
    Saturday, June 18, 2016 3:26 AM
  • Li's post is correct.

    Insert you should be, best practice etc to taste in the following:

    Handle the window closing in the window.

    Inject anything you need to do before it closes into some method which it runs.

    Maybe it needs to check a bunch of predicates before allowing itself to close.

    And these should get to the window via binding so everything is loosely coupled.

    With WPF think decoupling your variable logic from the view.

    Think binding.

    Your usercontrol should be inheriting a viewmodel which is the datacontext of your window or have it's own viewmodel.

    Either way they should bind to some property which you have in your window and that window closing should be grabbing it's pre-close criteria/processing from that property ( or properties ) and applying them without knowing in advance what they are.

    This could be as simple as an IsDirty flag, that could be just one property of it's getter could rely on  a list of predicates.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Saturday, June 18, 2016 8:30 AM