none
Datagrid and Contextmenu - Get the item which was right clicked

    Question

  • I'm wondering how to get the datagrid item which was right clicked. I've done quite a lot of research, and it points to my databinding. However, I'm unsure how to bind it correctly.

      <DataGrid x:Name="gridApplications" Grid.Row="0" Height="430" ItemsSource="{Binding InstalledApplications}" BorderThickness="0" HorizontalGridLinesBrush="{x:Null}" VerticalGridLinesBrush="{x:Null}"
    			 RowHeaderWidth="0" IsReadOnly="True" SelectionMode="Single" GridLinesVisibility="None" AutoGenerateColumns="False" Background="WhiteSmoke" ColumnHeaderStyle="{DynamicResource ListColumnHeaderStyle}" AlternatingRowBackground="White" 
    			 RowBackground="#F5F5F5F5" FontSize="14" Foreground="#FF333333" RowStyle="{DynamicResource ComputerInfoRowStyleTemplate}" Tag="{Binding ElementName=UserControl}" CellStyle="{DynamicResource DataGridCellStyle}" >
    
    
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Name" SortDirection="Ascending" SortMemberPath="Name" Binding="{Binding Name, Converter={StaticResource NameStringConverter}}" MinWidth="100" MaxWidth="500"/>
                    <!--<DataGridTextColumn Header="Publisher" SortMemberPath="Publisher" Binding="{Binding Publisher}" MinWidth="100"/>-->
                    <DataGridTextColumn Header="Version" SortMemberPath="Version" Binding="{Binding Version}" MinWidth="80"/>
                    <!--<DataGridTextColumn Header="Install date" SortMemberPath="InstallDate" Binding="{Binding InstallDate}" MinWidth="80" />-->
                    <DataGridTextColumn Header="Architecture" SortMemberPath="Architecture" Binding="{Binding Architecture}" MinWidth="80" MaxWidth="100"/>
                    <DataGridTextColumn Header="MSI Guid" SortMemberPath="Guid" Binding="{Binding Guid, Converter={StaticResource GuidStringConverter}}" MinWidth="100"/>
                </DataGrid.Columns>
                <DataGrid.ContextMenu>
                    <ContextMenu ItemsSource="{Binding RelativeSource={RelativeSource Self}}">
                        <MenuItem Header="Repair" Click="RepairItem_Click" />
                        <MenuItem Header="Uninstall" Click="UnInstallItem" />
                    </ContextMenu>
                </DataGrid.ContextMenu>
    
            </DataGrid>
    

    As you can see I've added two click events, which at the moment looks like this:

     private void RepairItem_Click(object sender, System.Windows.RoutedEventArgs e)
            {
                var menuItem = e.OriginalSource as MenuItem;
                if(menuItem != null)
                {
                    var parentContextMenu = menuItem.CommandParameter as ContextMenu;
                    if(parentContextMenu != null)
                    {
                        
                    }
                }
            }
    


    The e.OriginalSource is always null for some reason. However, what I really hope to receive is an object of type ProgramObject.


     

    Wednesday, October 05, 2011 10:03 AM

Answers

All replies