How to display a new window by double clicking a row in datagrid in 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 project
  • Once added, add any datagrid, rows, columns you need to, example

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>