Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Window that hosts a Page. I want to fire an event on the Page when the user clicks cancel when closing the MainWindow. At the moment I am struggling to work out how to fire the event on the Page from Window. Here is the event on the Page;

C#
public void OnChangeUserEvent()
{
    var _employeeSelectionWindow = new EmployeeSelectionWindow();
    _employeeSelectionWindow.ReturnEmployeeDetails += LoadSelectedEmployeeAsLoggedInUser;
    _employeeSelectionWindow.ShowDialog();
}


And here is when I would like to fire it (this method is in a Window, hosting the page);

C#
private void OnWindowClosed(object sender, System.ComponentModel.CancelEventArgs e)
{
    var result = MessageBox.Show(@"Are you sure you want to exit the database?" + Environment.NewLine + "Click cancel to change user.",
                                "", MessageBoxButton.YesNoCancel);
    if (result == MessageBoxResult.Yes)
    {
        Environment.Exit(0);
    }
    else if (result == MessageBoxResult.Cancel)
    {
        //Fire the event on the page here!
        e.Cancel = true;
    }
    else
    {
        e.Cancel = true;
    }
}


What I have tried:

I've tried using something like;

C#
((LoginPage)e.Content).UserChangeEvent+= new EventHandler(MainWindow_NewRecordSaved);


nothing has worked for me so far.
Posted
Updated 5-Sep-16 0:30am

1 solution

Try this following sample:

Main window markup
XML
<Window x:Class="WpfApp.MainWindow"
        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:WpfApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="ShowPageButton" Content="Show Page" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="ShowPageButton_Click"/>
        <Frame x:Name="frame" Content="Frame" HorizontalAlignment="Left" Height="247" Margin="10,37,0,0" VerticalAlignment="Top" Width="507"/>
        <Button x:Name="CancelButton" Content="Cancel" HorizontalAlignment="Left" Margin="414,300,0,0" VerticalAlignment="Top" Width="76" Click="CancelButton_Click"/>
    </Grid>
</Window>

Main window Code
C#
public partial class MainWindow : Window
    {
        private Page1.OnMainWindowEvent CancelEvent;
        private Page1 page1;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void ShowPageButton_Click(object sender, RoutedEventArgs e)
        {
            CancelEvent = new Page1.OnMainWindowEvent(OnCancelClicked);
            page1 = new Page1();
            page1.RegisterMainWindowEvent(CancelEvent);
            frame.Content = page1;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            OnCancelClicked("Cancel clicked on main window.");
        }

        private void OnCancelClicked(string message)
        {
            page1.DoSomething(message);
        }
    }

Page1 markup
XML
<Page x:Class="WpfApp.Page1"
      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:WpfApp"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="Page1">

    <Grid>
        <Label x:Name="label" Content="Hi I am Page1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>

    </Grid>
</Page>

Page1 code
C#
public partial class Page1 : Page
    {
        public delegate void OnMainWindowEvent(string eventName);
        private event OnMainWindowEvent MainWindowEvent;

        public Page1()
        {
            InitializeComponent();
        }

        public void RegisterMainWindowEvent(OnMainWindowEvent eventHandler)
        {
            MainWindowEvent = eventHandler;
        }

        public void DoSomething(string someMessage)
        {
            MessageBox.Show(someMessage);
            label.Content = someMessage;
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900