发表于33分钟前(固定链接) 早上好, 我想使用可以在GridView中拖放的TreeView 我制作TreeView但是当想要拖放我有一个问题时出现null异常,4天后我看不到问题。 让我告诉你我的代码来帮助我。 我有一个类库LabelData来创建数据库和数据类 这是我的表 CREATE TABLE MyTable (IdTable Int(2), IdLabel Int(20), LName char(50)) 和我的行 INSERT INTO MyTable(IdTable) ,IdLabel,LName)VALUES(1,200,'firstName'); INSERT INTO MyTable(IdTable,IdLabel,LName)VALUES(2,200,'lastName'); INSERT INTO MyTable(IdTable,IdLabel,LName)VALUES(3,200,'middleName'); INSERT INTO MyTable(IdTable,IdLabel,LName)VALUES(4,201,'circuit'); INSERT INTO MyTable(IdTable,IdLabel,LName)VALUES(5,201,'series'); INSERT INTO MyTable(IdTable,IdLabel,LName)VALUES(6,202,'country'); INSERT INTO MyTable(IdTable,IdLabel,LName)VALUES(7,202,'region'); INSERT INTO MyTable(IdTable,IdLabel,LName)VALUES(8,202,'zipcode'); 为了进行树视图,我使用IdLabel制作像类别,我使用LName制作产品。 我在我的桌子上添加一个遗产MyTable GroupLabel,属性LName 这是我的班级data.cs 使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System.Text; LNamespace LabelData { public class DataProvider { LabelDataContext dc; public DataProvider() { dc = new LabelDataContext(); } public IEnumerable< Label> GetLabel() {从e.IdLabel的dc.MyTable 组e中返回到gr 选择新标签(){IdLabel = gr.Key,LName = gr }; } public IEnumerable< MyTable> Get_Table() {从dc返回p.TyTable select p; } } 公共类标签 { public int IdLabel {得到;组; } public IEnumerable< MyTable> LName {get;组; } } } 这是我的代码xaml.cs 使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System.Text;使用System.Windows ; 使用System.Windows.Data; 使用System.Windows.Documents; 使用System.Windows.Input; 使用System.Windows.Media; 使用System.Windows.Media.Imaging;使用System.Windows.Navigation ; 使用System.Windows.Shapes; 使用Telerik.Windows.Controls.GridView; 使用Telerik.Windows.Controls.DragDrop; 使用Telerik.Windows.Controls; 使用System.Collections.ObjectModel; 使用LabelData; 使用Telerik.Windows.Controls.TreeView; 使用System.Collections; 使用System.Net;使用System.Windows.Media.Animation ;使用Telerik.Windows ; LNamespace LabelMunich { ///< summary> /// Logique d'interaction pour MainWindow.xaml ///< / summary> public partial class WindowTreeView:Window { public WindowTreeView() { InitializeComponent(); ProfilData.DataProvider dataProvider = new ProfilData.DataProvider(); treeView1.ItemsSource = dataProvider.GetModule(); radGridView.ItemsSource = dataProvider.Get_Table(); RadDragAndDropManager.AddDropQueryHandler(radGridView,new EventHandler< DragDropQueryEventArgs>(this.RadGridView_OnDropQuery)); RadDragAndDropManager.AddDropInfoHandler(radGridView,new EventHandler< DragDropEventArgs>(this.RadGridView_OnDropInfo)); } private void RadRibbonButton_Click(object sender,RoutedEventArgs e) { // FAjout fa = new FAjout(); formAjout fa = new formAjout(); fa.Show(); // this.ShowActivated(); } private void radTreeView_PreviewDragEnded(object sender,RadTreeViewDragEndedEventArgs e) { e.Handled = true; } private void RadGridView_OnDropQuery(object sender,DragDropQueryEventArgs e) { e.QueryResult =(e.Options.Source as RadTreeViewItem)。项目是模块; } private void RadGridView_OnDropInfo(object sender,DragDropEventArgs e) { if(e.Options.Status == DragStatus.DropComplete) { (radGridView.ItemsSource为IList< Module>)。添加((e.Options.Payload as Collection< Object>)[0]作为模块); } } } } 和我的Xaml文件 < pre lang =xml>< Window x:Class =ProfilTandem.WindowTreeView xmlns =http:// schemas。 microsoft.com/winfx/2006/xaml/presentation xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml xmlns:northwind =clr-namespace: ProfilData; assembly = LabelData xmlns:local =clr-namespace:LabelMunich xmlns:telerik =http://schemas.telerik.com/2008/xaml/presentation xmlns:telerikDragDrop =clr-namespace:Telerik.Windows.Controls.DragDrop; assembly = Telerik.Windows.Controls Title =MainWindowHeight =450Width =585> < Window.Resources> < DataTemplate x:Key =Team> < TextBlock Text ={Binding Path = LName}/> < / DataTemplate> < HierarchicalDataTemplate DataType ={x:Type northwind:Label}ItemsSource ={Binding Path = LName}> < Border Width =250> < Grid> < TextBlock Text ={Binding Path = IdLabel}FontSize =14FontWeight =BoldMargin =2> < / TextBlock> < / Grid> < / Border> < / HierarchicalDataTemplate> < HierarchicalDataTemplate DataType ={x:Type northwind:GroupLabel}> < Grid Margin =6> < TextBlock Text ={Binding Path = LName}Padding =2/> < / Grid> < / HierarchicalDataTemplate> < /Window.Resources> < Grid> < Grid.ColumnDefinitions> < ColumnDefinition Width =*/> < ColumnDefinition Width =*/> < /Grid.ColumnDefinitions> < Grid.RowDefinitions> < RowDefinition Height =Auto/> < RowDefinition Height =Auto/> < /Grid.RowDefinitions> < telerik:RadTreeView IsDragDropEnabled =TrueGrid.Column =0Grid.Row =1 SelectionMode =MultipleIsEditable =Truex:Name = treeView1Margin =0,20,0,245 PreviewDragEnded =radTreeView_PreviewDragEnded IsDragTooltipEnabled =FalseItemsSource ={Binding}/> < StackPanel Grid.Column =1Grid.Row =1> < telerik:RadGridView x:Name =radGridViewAutoGenerateColumns =False Width =400Height =400HorizontalAlignment =LefttelerikDragDrop:RadDragAndDropManager.AllowDrop =True DataContext ={Binding}ItemsSource ={Binding}> < telerik:RadGridView.Columns> < telerik:GridViewDataColumn DataMemberBinding ={Binding Path = IdLabel}/> < telerik:GridViewDataColumn DataMemberBinding ={Binding Path = LName}/> < / telerik:RadGridView.Columns> < / telerik:RadGridView> < / StackPanel> < / Grid> < / Window> 这就是全部 当我尝试将项目树视图拖到gridview时,我在这条线上有例外 (radGridView.ItemsSource为IList< Label>)。添加((e.Options.Payload as Collection< Object>)[0] as Label); 异常System.NullReferenceException未处理 对象引用未设置为对象的实例 感谢您的帮助解决方案 只需运行调试器即可。它看起来像 radGridView.ItemsSource 是 null 或者它的类型没有实现接口 IList< ; Label> ,它将返回as运算符的null结果。检查您分配 ItemsSource 的位置及其类型。在你的代码中,不要在这一行中做出隐含的假设。 -SA 你可以找到一篇在这里完成这项工作的文章。 http://gonetdotnet.blogspot.in/2014/09/solved-how-to-drag-and-drop-items-from.html Good morning,I would like to use a TreeView that's can drag and drop in a GridViewI make the TreeView but when a want to drag and drop i have a probleme the null Exception, after 4 days i can't see the problem.Let me show you my code to help me please.I have a class library LabelData to make the database and a data classthis is my table<pre lang="sql">CREATE TABLE MyTable(IdTable Int(2),IdLabel Int(20),LName char(50))<pre lang="sql">INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (1, 200, 'firstName');INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (2, 200, 'lastName');INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (3, 200, 'middleName');INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (4, 201, 'circuit');INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (5, 201, 'series');INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (6, 202, 'country');INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (7, 202, 'region');INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (8, 202, 'zipcode');For make the treeview, I use IdLabel to make like "categorie" and i use LName to make like "product" .I add a heritance in on my table MyTable GroupLabel with the property LNameThis is my class data.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;LNamespace LabelData{ public class DataProvider { LabelDataContext dc; public DataProvider() { dc = new LabelDataContext(); } public IEnumerable<Label> GetLabel() { return from e in dc.MyTable group e by e.IdLabel into gr select new Label() { IdLabel = gr.Key, LName= gr }; } public IEnumerable<MyTable> Get_Table() { return from p in dc.MyTable select p; } } public class Label { public int IdLabel { get; set; } public IEnumerable<MyTable> LName{ get; set; } }}Goldoni Posted 33 minutes ago (permalink)Good morning,I would like to use a TreeView that's can drag and drop in a GridViewI make the TreeView but when a want to drag and drop i have a probleme the null Exception, after 4 days i can't see the problem.Let me show you my code to help me please.I have a class library LabelData to make the database and a data classthis is my tableCREATE TABLE MyTable(IdTable Int(2),IdLabel Int(20),LName char(50))and my RowsINSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (1, 200, 'firstName');INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (2, 200, 'lastName');INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (3, 200, 'middleName');INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (4, 201, 'circuit');INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (5, 201, 'series');INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (6, 202, 'country');INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (7, 202, 'region');INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (8, 202, 'zipcode');For make the treeview, I use IdLabel to make like "categorie" and i use LName to make like "product" .I add a heritance in on my table MyTable GroupLabel with the property LNameThis is my class data.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;LNamespace LabelData{ public class DataProvider { LabelDataContext dc; public DataProvider() { dc = new LabelDataContext(); } public IEnumerable<Label> GetLabel() { return from e in dc.MyTable group e by e.IdLabel into gr select new Label() { IdLabel = gr.Key, LName= gr }; } public IEnumerable<MyTable> Get_Table() { return from p in dc.MyTable select p; } } public class Label { public int IdLabel { get; set; } public IEnumerable<MyTable> LName{ get; set; } }}And this is my behind code xaml.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using Telerik.Windows.Controls.GridView;using Telerik.Windows.Controls.DragDrop;using Telerik.Windows.Controls;using System.Collections.ObjectModel;using LabelData;using Telerik.Windows.Controls.TreeView;using System.Collections;using System.Net;using System.Windows.Media.Animation;using Telerik.Windows;LNamespace LabelMunich{ /// <summary> /// Logique d'interaction pour MainWindow.xaml /// </summary> public partial class WindowTreeView : Window { public WindowTreeView() { InitializeComponent(); ProfilData.DataProvider dataProvider = new ProfilData.DataProvider(); treeView1.ItemsSource = dataProvider.GetModule(); radGridView.ItemsSource = dataProvider.Get_Table(); RadDragAndDropManager.AddDropQueryHandler(radGridView, new EventHandler<DragDropQueryEventArgs>(this.RadGridView_OnDropQuery)); RadDragAndDropManager.AddDropInfoHandler(radGridView, new EventHandler<DragDropEventArgs>(this.RadGridView_OnDropInfo)); } private void RadRibbonButton_Click(object sender, RoutedEventArgs e) { // FAjout fa = new FAjout(); formAjout fa = new formAjout(); fa.Show(); // this.ShowActivated(); } private void radTreeView_PreviewDragEnded(object sender, RadTreeViewDragEndedEventArgs e) { e.Handled = true; } private void RadGridView_OnDropQuery(object sender, DragDropQueryEventArgs e) { e.QueryResult = (e.Options.Source as RadTreeViewItem).Item is Module; } private void RadGridView_OnDropInfo(object sender, DragDropEventArgs e) { if (e.Options.Status == DragStatus.DropComplete) { (radGridView.ItemsSource as IList<Module>).Add((e.Options.Payload as Collection<Object>)[0] as Module); } } }}And my Xaml document<pre lang="xml"><Window x:Class="ProfilTandem.WindowTreeView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:northwind="clr-namespace:ProfilData;assembly=LabelData" xmlns:local="clr-namespace:LabelMunich" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" xmlns:telerikDragDrop="clr-namespace:Telerik.Windows.Controls.DragDrop;assembly=Telerik.Windows.Controls" Title="MainWindow" Height="450" Width="585"> <Window.Resources> <DataTemplate x:Key="Team"> <TextBlock Text="{Binding Path=LName}"/> </DataTemplate> <HierarchicalDataTemplate DataType="{x:Type northwind:Label}" ItemsSource="{Binding Path=LName}"> <Border Width="250"> <Grid> <TextBlock Text="{Binding Path=IdLabel}" FontSize="14" FontWeight="Bold" Margin="2" > </TextBlock> </Grid> </Border> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type northwind:GroupLabel}"> <Grid Margin="6"> <TextBlock Text="{Binding Path=LName}" Padding="2" /> </Grid> </HierarchicalDataTemplate> </Window.Resources> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <telerik:RadTreeView IsDragDropEnabled="True" Grid.Column="0" Grid.Row="1" SelectionMode="Multiple" IsEditable="True" x:Name="treeView1" Margin="0,20,0,245" PreviewDragEnded="radTreeView_PreviewDragEnded" IsDragTooltipEnabled="False" ItemsSource="{Binding}"/> <StackPanel Grid.Column="1" Grid.Row="1"> <telerik:RadGridView x:Name="radGridView" AutoGenerateColumns="False" Width="400" Height="400" HorizontalAlignment="Left" telerikDragDrop:RadDragAndDropManager.AllowDrop="True" DataContext="{Binding}" ItemsSource="{Binding}"> <telerik:RadGridView.Columns> <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=IdLabel}"/> <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=LName}"/> </telerik:RadGridView.Columns> </telerik:RadGridView> </StackPanel> </Grid></Window>This is allWhen i try to drag items treeview to gridview i have exception on this line(radGridView.ItemsSource as IList<Label>).Add((e.Options.Payload as Collection<Object>)[0] as Label);The exception System.NullReferenceException was unhandledObject reference not set to an instance of an objectThank you for your help 解决方案 Just run with the debugger ans see. It looks like either radGridView.ItemsSource is null or its type does not implement the interface IList<Label> which would return null result of the as operator. Check where you assign ItemsSource and its type. In your code, don't make implicit assumptions you made in this line.—SAyou can find an article which does this job here.http://gonetdotnet.blogspot.in/2014/09/solved-how-to-drag-and-drop-items-from.html 这篇关于拖放树视图到gridview的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 01:31