0

I have a c# .net4 project linked to an EF MySql database model. The main window has a data grid which shows the "EnteredTickets" the user has added to the MySql data base. When the user double clicks on the data grid row of one of the "EnteredTickets" a window opens (called edit) to allow user to edit the "Entered Ticket" and then "save" the edit. Once the edit is saved is completed the "edit" window closes.

What I want to do is refresh the data grid on the main window to show the edited data.

To do this after reading around I believe I need to raise an event and then "handle" this event in the MainWindowxmal.cs. In particular I am following the steps here. As you will see I have added a messagebox to know the event has been raise from the edit window. However I am not handling the event correctly in the mainwindow as the messagebox I have added in the event handler is not displaying "Edit Finished", This is the code I have

In the Edit Window

public partial class EditTicket : Window
{

    //public event EventHandler EditFinished;
    // Create a custom routed event by first registering a RoutedEventID
    // This event uses the bubbling routing strategy
    public static readonly RoutedEvent EditFinishedEvent = EventManager.RegisterRoutedEvent(
   "EditFinished", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(EditTicket));

 // Provide CLR accessors for the event
    public event RoutedEventHandler EditFinished
    {
        add { AddHandler(EditFinishedEvent, value); }
        remove { RemoveHandler(EditFinishedEvent, value); }
    }
// This method raises the EditFinished event
    void RaiseEditFinishedEvent()
    {
        RoutedEventArgs newEventArgs = new RoutedEventArgs(EditTicket.EditFinishedEvent);
        RaiseEvent(newEventArgs);
        MessageBox.Show("Raised Routed");
    }

The event is raised in the "save" button click by the following code and I get the Messagebox "Raised Routed"

  //raise editfinshed event
            RaiseEditFinishedEvent();

My problems occur on the Main Window side

The code in the Main Window

 public MainWindow()
    {
        InitializeComponent();

       AddHandler(EditTicket.EditFinishedEvent, new RoutedEventHandler(RefreshEnteredTickets));
        Application.Current.MainWindow.WindowState = WindowState.Maximized;
    }

My handler code is this but not actions

 void RefreshEnteredTickets(object sender, RoutedEventArgs e)
    {
     MessageBox.Show ("Edit Finished");
    }

Can any one suggest where I have gone wrong please?

Ian W
  • 385
  • 2
  • 10
  • 30
  • If you dont use MVVM, then u can check that: http://stackoverflow.com/questions/803242/understanding-events-and-event-handlers-in-c-sharp?rq=1 – Ugur Mar 13 '16 at 22:42
  • Ok thanks. No all behind code only so far. Not moved to MVVM – Ian W Mar 13 '16 at 22:54

1 Answers1

1

Hi try to use the simple registration like this:

Xaml code (main window)

<Window x:Class="RoutedEventSoHelpAttempt.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Click="ButtonBase_OnClick">Show Edit Window</Button>
</Grid>

Xaml code (edit ticket window)

<Window x:Class="RoutedEventSoHelpAttempt.EditTicket"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="EditTicket" Height="300" Width="300">
<Grid>
    <Button Content="Edit" Click="ButtonBase_OnClick"></Button>
</Grid>

Code behind

    /// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    EditTicket editTicket=new EditTicket();

    public MainWindow()
    {
        InitializeComponent();
        Loaded += OnLoaded;
        Unloaded += OnUnloaded;
    }

    private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
    {
        Loaded -= OnLoaded;
        Unloaded -= OnUnloaded;
        editTicket.EditFinished -= RefreshEnteredTickets;

    }

    private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        editTicket.EditFinished += RefreshEnteredTickets;
    }


    private void RefreshEnteredTickets(object sender, RoutedEventArgs e)
    {

    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        editTicket.Show();
    }
}

    /// <summary>
/// Interaction logic for EditTicket.xaml
/// </summary>
public partial class EditTicket : Window
{
    //public event EventHandler EditFinished;
    // Create a custom routed event by first registering a RoutedEventID
    // This event uses the bubbling routing strategy
    public static readonly RoutedEvent EditFinishedEvent;

    static EditTicket()
    {
        EditFinishedEvent = EventManager.RegisterRoutedEvent(
            "EditFinished", RoutingStrategy.Bubble, typeof (RoutedEventHandler), typeof (EditTicket));
    }

    public EditTicket()
    {
        InitializeComponent();
    }



    // Provide CLR accessors for the event
    public event RoutedEventHandler EditFinished
    {
        add { AddHandler(EditFinishedEvent, value); }
        remove { RemoveHandler(EditFinishedEvent, value); }
    }
    // This method raises the EditFinished event
    void RaiseEditFinishedEvent()
    {
        RoutedEventArgs newEventArgs = new RoutedEventArgs(EditFinishedEvent, this);
        RaiseEvent(newEventArgs);
        MessageBox.Show("Raised Routed");
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        RaiseEditFinishedEvent();
    }
}

In addition you can look at the next link: here

Let me know if you need more help.

Regards.

Ilan
  • 2,762
  • 1
  • 13
  • 24
  • Many thanks I will look at the link and work on this when back from work tonight – Ian W Mar 14 '16 at 08:08
  • Worked like a charm... but what is the following code do as it looks like a constructor ' static EditTicket() { EditFinishedEvent = EventManager.RegisterRoutedEvent( "EditFinished", RoutingStrategy.Bubble, typeof (RoutedEventHandler), typeof (EditTicket)); }; – Ian W Mar 14 '16 at 16:23
  • @IanW since the EditFinishedEvent is a static, I have to initialize it in the static c'tor, here is a link to additional information - http://www.c-sharpcorner.com/uploadfile/AmitSaxena/constructor-vs-static-constructor/. but you can remove this and initialize the EditFinishedEvent inline. – Ilan Mar 15 '16 at 06:57