1.首先构造实体类Team
- public class Team
- {
- public Team(string name)
- {
- this.Name = name;
- }
- string Name { get; set; }
- }
2.构造实体类
class Division
{
public Division( {
this.Teams = new ObservableCollection<Team>();
}
set; }
public ObservableCollection<Team> Teams { }
3.构造实体类League
class League
public League( this.Divisions = new ObservableCollection<Division>();
}
string Name
{
get;
set;
public ObservableCollection<Division> Divisions
get;
set;
}
4.构造一个提供数据的类RadTreeViewSampleData
class RadTreeViewSampleData
public RadTreeViewSampleData()
this.InitializeLeaguesDataSource();
public ObservableCollection<League> LeaguesDataSource
private void InitializeLeaguesDataSource()
this.LeaguesDataSource = new ObservableCollection<League>();
League l;
Division d;
this.LeaguesDataSource.Add(l = new League("League A"));
l.Divisions.Add((d = new Division("Division A")));
d.Teams.Add(new Team("Team I"));
d.Teams.Add(new Team("Team II"));
new Team("Team III"));
new Team("Team IV"));
new Team("Team V"));
new Division("Division B")));
new Team("Team Blue"));
new Team("Team Red"));
new Team("Team Yellow"));
new Team("Team Green"));
new Team("Team Orange"));
new Division("Division C")));
new Team("Team East"));
new Team("Team West"));
new Team("Team north"));
new Team("Team South"));
new League("League B"));
new Team("Team 1"));
new Team("Team 2"));
new Team("Team 3"));
new Team("Team 4"));
new Team("Team 5"));
new Team("Team Diamond"));
new Team("Team Heart"));
new Team("Team Club"));
new Team("Team Spade"));
l.Divisions.Add((d = new Division("Division C")));
new Team("Team Alpha"));
new Team("Team Beta"));
new Team("Team Gamma"));
new Team("Team Delta"));
new Team("Team Epsilon"));
}
5.定义页面
<UserControl.Resources> <sampleData:RadTreeViewSampleData x:Key="DataSource"/> <DataTemplate x:Key="Team"> <TextBlock Text="{Binding Name}"/> </DataTemplate> <telerik:HierarchicalDataTemplate x:Key="Division" ItemsSource="{Binding Teams}" ItemTemplate="{StaticResource Team}"> <TextBlock Text="{Binding Name}"/> </telerik:HierarchicalDataTemplate> <telerik:HierarchicalDataTemplate x:Key="League" ItemsSource="{Binding Divisions}" ItemTemplate="{StaticResource Division}"> <TextBlock Text="{Binding Name}"/> </telerik:HierarchicalDataTemplate> <DataTemplate x:Key="ListBoxDataTemplate"> <TextBlock Text="{Binding Name}"/> </DataTemplate> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White" Margin="8"> <Grid.ColumnDeFinitions> <ColumnDeFinition Width="120"/> <ColumnDeFinition Width="100"/> <ColumnDeFinition Width="*"/> </Grid.ColumnDeFinitions> <Grid.RowDeFinitions> <RowDeFinition Height="Auto"/> <RowDeFinition/> </Grid.RowDeFinitions> <TextBlock Text="RadTreeView" FontSize="16" Margin="5" VerticalAlignment="Bottom" /> <TextBlock Text="TextBox" FontSize="16" Grid.Column="1" VerticalAlignment="Bottom" Margin="5" /> <TextBlock Text="RadGridView" FontSize="16" Grid.Column="2" VerticalAlignment="Bottom" Margin="5" /> <telerik:RadTreeView x:Name="radTreeView" IsDragDropEnabled="True" Margin="0,8,0" IsDragTooltipEnabled="False" Grid.Row="1" ItemsSource="{Binding Source={StaticResource DataSource},Path=LeaguesDataSource}" ItemTemplate="{StaticResource League}"/> <TextBox x:Name="textBox" Grid.Column="1" Grid.Row="1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="90" Margin="0,0"/> <telerik:RadGridView x:Name="radGridView" Grid.Column="2" Grid.Row="1" Height="280" Margin="0,0" VerticalAlignment="Top" AutoGenerateColumns="False" ItemsSource="{Binding Source={StaticResource DataSource},Path=EmptyTeamDataSource}" SelectionChanged="radGridView_SelectionChanged"> <telerik:RadGridView.Columns> <telerik:GridViewDataColumn DataMemberBinding="{Binding Id}"/> <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"/> <telerik:GridViewDataColumn DataMemberBinding="{Binding ImageUrl}"/> </telerik:RadGridView.Columns> </telerik:RadGridView> </Grid>
6.定义变量与构造函数,其中设置textBox,radgridView允许拖拉并绑定drop的命令
private ObservableCollection<Team> aaa=new ObservableCollection<Team>(); public Tree2() { InitializeComponent(); //radGridView. RadDragAndDropManager.SetAllowDrop(textBox,true); RadDragAndDropManager.SetAllowDrop(radGridView,true); RadDragAndDropManager.AddDropQueryHandler(textBox,new EventHandler<DragDropQueryEventArgs>(this.TextBox_OnDropQuery)); RadDragAndDropManager.AddDropInfoHandler(textBox,new EventHandler<DragDropEventArgs>(this.TextBox_OnDropInfo)); RadDragAndDropManager.AddDropQueryHandler(radGridView,new EventHandler<DragDropQueryEventArgs>(this.RadGridView_OnDropQuery)); RadDragAndDropManager.AddDropInfoHandler(radGridView,new EventHandler<DragDropEventArgs>(this.RadGridView_OnDropInfo)); }
private void TextBox_OnDropQuery(object sender,DragDropQueryEventArgs e) { e.QueryResult = true; e.Options.ArrowCue = RadDragAndDropManager.GenerateArrowCue(); } private void TextBox_OnDropInfo(object sender,DragDropEventArgs e) { if (e.Options.Status == DragStatus.DropComplete) { object item = (e.Options.Payload as Collection<Object>)[0]; PropertyInfo info = item.GetType().GetProperty("Name"); textBox.Text = info == null ? String.Empty : info.GetValue(item,null).ToString(); } } private void RadGridView_OnDropQuery(object sender,DragDropQueryEventArgs e) { e.QueryResult = true; } private void RadGridView_OnDropInfo(object sender,DragDropEventArgs e) { if (e.Options.Status == DragStatus.DropComplete) { foreach (object dropItem in e.Options.Payload as Collection<Object>) { Team team = dropItem as Team; if (team != null) { aaa.Add(team); } this.radGridView.ItemsSource = aaa; //(this.radGridView.ItemsSource as ObservableCollection<Team>).Add(team); } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。