How to pass the name of a wpf window as parameter

Asked

Viewed 177 times

0

I use a userControl customized to show a Data Grid and when I double-click some line, I call a registration window, which is waiting to send the ID of the line selected as parameter, wanted to simplify by passing this method of all screens Data Grid to the userControl customized as virtual method, so in double click I would call only the method, only I cannot pass the window name as parameter.


Failed example of the userControl virtual method (in theory that would be)

public virtual void AbrirJanelaCadastro(Window window, DataGrid grid) 
{
    DataRow dataRow = (grid.SelectedItem as DataRowView).Row;
    window(dataRow.Field<Int32>("ID")).ShowDialog();
}

Example of method call to open window in double click

private void grid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
        AbrirJanelaCadastro(Cliente, Grid);
  • Explain why the code is "flawed". The question is not clear enough.

1 answer

1

when calling the Window, enter the window object to be opened and set the Title property

 Window1 w = new Window1();
 w.Title = "Seu Título";
 w.Show();

where is "Your Title"; you must place the value of your Datarow

in your code, it could look like this:

 DataRow dataRow = (grid.SelectedItem as DataRowView).Row;
 window.Title = dataRow.Field<Int32>("ID").ToString();
 window.ShowDialog();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.