DataGrid 中选定的行已丢失

我有一个 DataGrid,它们的行更新得非常快。每行都有一个右键单击事件,如果单击,则应将所选行添加到另一个数据网格中。问题是,当用户选择一行以便添加到另一个列表时,由于更新了所选行,他无法添加,因此用户应该一次又一次地选择该行。我的代码是这样的


       <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>

当我想从 contexMenu 单击“addtoReportedListMnuBtn”时,我应该尝试几次来执行其事件。通常通过下面的代码显示消息框


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");

        }

    }   


慕妹3242003
浏览 171回答 2
2回答

翻阅古今

我想说,DataGrid在你的例子中, a 是一个错误的地方ContextMenu。将您的ContextMenu资源放入窗口资源并将其用于DataGridCell.<Window.Resources>&nbsp; &nbsp; <ContextMenu x:Key="contMen" HorizontalContentAlignment="Right" FlowDirection="RightToLeft">&nbsp; &nbsp; &nbsp; &nbsp; <MenuItem Name="addToBlackListMnuBtn" Header="Add to Black List" Click="addToBlackListMnuBtn_Click" FontWeight="Black"/>&nbsp; &nbsp; &nbsp; &nbsp; <MenuItem Name="addtoReportedListMnuBtn" Header="Add to Reported List"&nbsp; Click="addtoReportedListMnuBtn_Click" FontWeight="Black"/>&nbsp; &nbsp; </ContextMenu></Window.Resources><DataGrid.Resources>&nbsp; &nbsp; <Style TargetType="DataGridCell">&nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="ContextMenu" Value="{StaticResource contMen}"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </Style></DataGrid.Resources>private void addtoReportedListMnuBtn_Click(object sender, RoutedEventArgs e)//add to reported list{&nbsp; &nbsp; var en = (((sender as MenuItem).Parent as ContextMenu).PlacementTarget as DataGridCell).DataContext as ObjClass;&nbsp; &nbsp; if (en != null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ReportSignalsListQ.Data = en; // add to queue for adding&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show("Please select again");&nbsp; &nbsp; }}

蝴蝶刀刀

当您ItemSource更新时,SelectedItem您的信息DataGrid将被清除。SelectionChanged因此,您可以向您添加一个事件DataGrid,并且需要将您分配SelectedItem给代码隐藏中的局部变量,然后将局部变量添加到第二个DataGrid。
打开App,查看更多内容
随时随地看视频慕课网APP