效果图:
1.首先构造实体类Team
- public class Team
- {
- public Team(string name)
- {
- this.Name = name;
- }
- public string Name { get; set; }
- }
2.构造实体类
- public class Division
- {
- public Division(string name)
- {
- this.Name = name;
- this.Teams = new ObservableCollection<Team>();
- }
- public string Name { get; set; }
- public ObservableCollection<Team> Teams { get; set; }
- }
3.构造实体类League
- public class League
- {
- public League(string name)
- {
- this.Name = name;
- this.Divisions = new ObservableCollection<Division>();
- }
- public string Name
- {
- get;
- set;
- }
- public ObservableCollection<Division> Divisions
- {
- get;
- set;
- }
- }
4.构造一个提供数据的类RadTreeViewSampleData
- public class RadTreeViewSampleData
- {
- public RadTreeViewSampleData()
- {
- this.InitializeLeaguesDataSource();
- }
- public ObservableCollection<League> LeaguesDataSource
- {
- get;
- set;
- }
- 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"));
- d.Teams.Add(new Team("Team III"));
- d.Teams.Add(new Team("Team IV"));
- d.Teams.Add(new Team("Team V"));
- l.Divisions.Add((d = new Division("Division B")));
- d.Teams.Add(new Team("Team Blue"));
- d.Teams.Add(new Team("Team Red"));
- d.Teams.Add(new Team("Team Yellow"));
- d.Teams.Add(new Team("Team Green"));
- d.Teams.Add(new Team("Team Orange"));
- l.Divisions.Add((d = new Division("Division C")));
- d.Teams.Add(new Team("Team East"));
- d.Teams.Add(new Team("Team West"));
- d.Teams.Add(new Team("Team North"));
- d.Teams.Add(new Team("Team South"));
- this.LeaguesDataSource.Add(l = new League("League B"));
- l.Divisions.Add((d = new Division("Division A")));
- d.Teams.Add(new Team("Team 1"));
- d.Teams.Add(new Team("Team 2"));
- d.Teams.Add(new Team("Team 3"));
- d.Teams.Add(new Team("Team 4"));
- d.Teams.Add(new Team("Team 5"));
- l.Divisions.Add((d = new Division("Division B")));
- d.Teams.Add(new Team("Team Diamond"));
- d.Teams.Add(new Team("Team Heart"));
- d.Teams.Add(new Team("Team Club"));
- d.Teams.Add(new Team("Team Spade"));
- l.Divisions.Add((d = new Division("Division C")));
- d.Teams.Add(new Team("Team Alpha"));
- d.Teams.Add(new Team("Team Beta"));
- d.Teams.Add(new Team("Team Gamma"));
- d.Teams.Add(new Team("Team Delta"));
- d.Teams.Add(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,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,8,0,0"/>
<telerik:RadGridView x:Name="radGridView" Grid.Column="2" Grid.Row="1"
Height="280" Margin="0,8,8,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);
}
}
}