0

Currently I have a datagrid that is populated with database info. When you double click a row I have been able to successfully handle programically but now I am trying to tackle passing data. Is there anyway to accomplish this using my current setup or do I need to re-write the way I am handling the double click.

Here is the XAML.

<DataGrid Name="DgMain" HorizontalAlignment="Stretch" Height="auto" Margin="10,43" VerticalAlignment="Stretch" Width="auto" 
              AutoGenerateColumns="True" ItemsSource="{Binding}" SelectionChanged="DgMain_SelectionChanged"
              SelectedItem="{Binding SelectedItem}" SelectionMode="Single" SelectionUnit="FullRow" IsSynchronizedWithCurrentItem="True" CanUserAddRows="False" CanUserDeleteRows="False"
              IsReadOnly="True" EnableRowVirtualization="True">
        <DataGrid.Resources>
            <Style TargetType="DataGridRow">
                <EventSetter Event="MouseDoubleClick" Handler="DgMain_DoubleClick"/>
            </Style>
        </DataGrid.Resources>
    </DataGrid>

Here is the code behind.

private void DgMain_DoubleClick(object sender, MouseButtonEventArgs e)
    {

        MessageBox.Show(sender.ToString());
    }

This code works, I get the Messagebox showing that the sender is the DataGrid, I am stuck on how to check the item that was click though. Thanks in advance for any help.

G_Man
  • 1,323
  • 2
  • 21
  • 33
  • IMHO, you should be using utilizing MVVM. Anyways, create a class that would hold your data, then construct this class with your columns/rows data and pass this off... I also see `SelectedItem` is this a property or? `SelectedItem` is what you want to go after once you double click the row... – Trevor May 13 '19 at 20:23
  • I am kind of stumbling into WPF from a windows form world so I really have no idea what I am doing, I will need to determine how to use MVVM with WPF. Appreciate pointing me in that direction. – G_Man May 13 '19 at 20:28
  • Would the clicked item be the SelectedItem that you have bound in the XAML? – Rob Goodwin May 13 '19 at 20:39
  • Yes that is correct @RobGoodwin – G_Man May 13 '19 at 20:44
  • 1
    Thank you @Çöđěxěŕ that post helped me immensely, I was able to do exactly as you said and use the table class I had already created and pulled from DgMan.SelectedItem to dump the data into a new instance of that class. – G_Man May 13 '19 at 21:00
  • @G_Man you are welcome, glad that helped you solve your issue. – Trevor May 14 '19 at 13:02

0 Answers0