0
votes

I have a DataGrid that their rows updated very quickly . every row has a right click event that if you click,this selected row should be added to another dataGrid. the problem is when user selected a row inorder to add to another list because of updating the selected row ,he couldnt add so user should be select the row again and again . my code is something like this

       <DataGrid SelectionMode="Single" CanUserAddRows="False"CanUserDeleteRows="False"  >

            <DataGrid.Columns>
                <DataGridTextColumn   Header="Data1"  Binding="{Binding Path=Data1}"></DataGridTextColumn>
                <DataGridTextColumn   Header="Data2"  Binding="{Binding Path=Data2}"></DataGridTextColumn>
                <DataGridTextColumn   Header="Data3"  Binding="{Binding Path=Data3}"></DataGridTextColumn>
                <DataGridTextColumn   Header="Data4" Binding="{Binding Path=Data4}"></DataGridTextColumn>
                <DataGridTextColumn   Header="Data5" Binding="{Binding Path=Data5}"></DataGridTextColumn>
                <DataGridTextColumn   Header="Data6" Binding="{Binding Path=Data6}"></DataGridTextColumn>

            </DataGrid.Columns>

            <DataGrid.ContextMenu>
                <ContextMenu HorizontalContentAlignment="Right" FlowDirection="RightToLeft">

                    <MenuItem Name="addToBlackListMnuBtn" Header="Add to Black List" Click="addToBlackListMnuBtn_Click" FontWeight="Black"/>
                    <MenuItem Name="addtoReportedListMnuBtn" Header="Add to Reported List"  Click="addtoReportedListMnuBtn_Click" FontWeight="Black"/>

                </ContextMenu>
            </DataGrid.ContextMenu>

        </DataGrid>

When I want to click "addtoReportedListMnuBtn" from contexMenu I should try several time for doing its event. usually show messageBox form the code below

private void addtoReportedListMnuBtn_Click(object sender, RoutedEventArgs e)//add to reported list
    {
        ObjClass en = (ObjClass)ActiveSignalDataGrid.SelectedItem;
        if(en!=null)
        {
            ReportSignalsListQ.Data = en; // add to queue for adding 
        }else
        {
            MessageBox.Show("Please select again");
        }
    }   
2
In your question list because of updating the selected row you mean you are updating another datagrid selection ? - Anant Dabhi
No , this current list updates very quickly , and you when you right click in a specific row and select addToReportedList from its contextMenu .messageBox has been shown by its Event - Hadi Mirzaei

2 Answers

0
votes

I would say, that a DataGrid is a wrong place in your case for the ContextMenu. Put your ContextMenu to the window's ressources and use it for a DataGridCell.

<Window.Resources>
    <ContextMenu x:Key="contMen" HorizontalContentAlignment="Right" FlowDirection="RightToLeft">
        <MenuItem Name="addToBlackListMnuBtn" Header="Add to Black List" Click="addToBlackListMnuBtn_Click" FontWeight="Black"/>
        <MenuItem Name="addtoReportedListMnuBtn" Header="Add to Reported List"  Click="addtoReportedListMnuBtn_Click" FontWeight="Black"/>
    </ContextMenu>
</Window.Resources>


<DataGrid.Resources>
    <Style TargetType="DataGridCell">
        <Setter Property="ContextMenu" Value="{StaticResource contMen}"/>                        
    </Style>
</DataGrid.Resources>

private void addtoReportedListMnuBtn_Click(object sender, RoutedEventArgs e)//add to reported list
{
    var en = (((sender as MenuItem).Parent as ContextMenu).PlacementTarget as DataGridCell).DataContext as ObjClass;
    if (en != null)
    {
        ReportSignalsListQ.Data = en; // add to queue for adding 
    }
    else
    {
        MessageBox.Show("Please select again");
    }
}
0
votes

When your ItemSource updates the SelectedItem of your DataGrid is cleared. So you can add a SelectionChanged event to your DataGrid and you need to assign your SelectedItem to a local variable in the code-behind and then add the local variable to the second DataGrid.