How to display a new window by double clicking a row in datagrid in WPF

Posted on Posted in C#, WPF

An easy way of displaying a new window in WPF by double clicking a selected row in Data Grid

  • Add a new second window to projectWPF 1
  • Once added, add any datagrid, rows, columns you need to, example
  • WPF 2
  • Then go FirstWindow.xaml (main window)
  • Add MouseDoubleClick event<Grid><DataGrid Name=”uxDeclaration” AutoGenerateColumns=”False” IsReadOnly=”True” Margin=”0,5,0,0″  ItemsSource=”{Binding}” MouseDoubleClick=”uxDeclaration_MouseDoubleClick” >/Grid>
  • This will generate code behind eventprivate void uxDeclaration_MouseDoubleClick(object sender, MouseButtonEventArgs e){

     SecondWindow secondWin = new SecondWindow();                             secondWin.ShowDialog(); 

    }

  • First instantiate and then set it to ShowDialog will pop up a new window on double click
  • We can even pass an ID or anything we need to from first window to Second by something like belowSecondWindow secondWin = new SecondWindow(myContactId);

    But make sure our SecondWindow has a cconstructor which takes a parameter of that type