双击datagrid中的选定行无法正常工作

时间:2018-06-11 00:49:00

标签: c# wpf

当用户点击一排表时,我正在尝试创建树 这是选择改变的代码

     private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
    {
        DataGridRow row = sender as DataGridRow;
        // Some operations with this row
        if (row != null)
        {
            var item = row.DataContext as Processes;
            if (item != null)
            {
                int id = item.ID;
                var subModules = new List<ITreeNode>();
                var subThreads = new List<ITreeNode>();
                for (int i = 0; i <= 3; i++)
                {
                    subThreads.Add(new SubThread { Name = "subthread" + i });
                }
                var nodes = new List<ITreeNode>
    {
        new Thread { Name = "Thread ", ChildNodes = subThreads },
        new Module { Name = "Module ", ChildNodes = subModules }
    };
                var runprocesses = new List<RunProcesses>();
                Process myProcess = Process.GetProcessById(id);
                ProcessModule myProcessModule;
                //Get all the modules associated with 'myProcess".
                ProcessModuleCollection myProcessModuleCollection = myProcess.Modules;
                for (int i = 0; i < myProcessModuleCollection.Count; i++)
                {
                    myProcessModule = myProcessModuleCollection[i];
                    string s = myProcessModule.FileName;
                    if (i == 1)
                    {
                        runprocesses.Add(new RunProcesses { Name = myProcessModule.FileName, ChildNodes = nodes });
                    }

                    subModules.Add(new SubModule { Name = s });
                    //Console.WriteLine("The moduleName is " + myProcessModule.ModuleName);
                }

                TreeView.ItemsSource = runprocesses;

            }
        }
}

将双击事件处理程序添加到系统

public MainWindow()
    {
        InitializeComponent();
        Style rowStyle = new Style(typeof(DataGridRow));
        rowStyle.Setters.Add(new EventSetter(DataGridRow.MouseDoubleClickEvent,
                                 new MouseButtonEventHandler(Row_DoubleClick)));
        dg.RowStyle = rowStyle;//dg is the name of datagrid
    }

当我双击表格输出的任何一行时,该模块的调试信息中缺少源信息,并且System.InvalidOperationException:&#39;&#39; EditItem&#39;不允许使用此视图。&#39; 我从各种链接中搜索了解这个问题的原因和解决方法,但没有人解决我的问题 我无法理解我错在哪里。请指导我如何解决这个问题。

0 个答案:

没有答案