KHL
KHL

Reputation: 475

How to raise Datagrid RowEditEnding event from seperate window in WPF?

I have two separate windows in my WPF project , the first one contains a DataGrid and the second one contains some controls and an ok button .

What i want is when i click on the ok button , the RowEditEnding event of the DataGrid which is in the second window will be raised. Any idea please ?

Thanks in advance.

Upvotes: 0

Views: 546

Answers (2)

mm8
mm8

Reputation: 169150

A window can't raise the RowEditEnding of the DataGrid. It can only handle it. Only the DataGrid itself can actually raise the event.

If you move the code that you have written in the event handler to a standalone method, you can simply call this from the other window. You will need to get a reference to the window where the DataGrid is defined first.

Please refer to the following sample code.

Window 1:

private void DataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
    HandleEvent();
}

public void HandleEvent()
{
    //your logic...
}

Window 2:

Window1 win = Application.Current.Windows.OfType<Window1>().FirstOrDefault();
if (win != null)
    win.HandleEvent();

Upvotes: 1

Muhammad Imran khan
Muhammad Imran khan

Reputation: 153

try this

 private void Button_Click(object sender, RoutedEventArgs e)
{
  Window2 win = new Window2();
  win.Show();
  win.MyGrid.RaiseEvent(new RowEditEnding(sender,e));
}

Upvotes: 0

Related Questions