Sample download: http://1code.codeplex.com/releases/view/62253#DownloadId=215079
Amit Dey from Microsoft contributed a nice Silverlight code sample that demonstrates enabling CRUD (Create, Read, Update, Delete) and drag & drop in Silverlight TreeView control. Silverlight TreeView control with CRUD and drag & drop is a frequently asked programming question in Silverlight forums. Many customers also requested this code sample in our code sample request service. We hope that this sample can reduce developers' efforts in handling this typical programming scenario.
Thanks to Amit!
http://deyamit.wordpress.com/2011/02/14/silverlight-treeview-control-with-crud/
This is a code sample for a Silverlight TreeView Control which supports CRUD ( Create, Read, Update, Delete ) operations. In addition it supports Drag & Drop of items. This post assumes that you have atleast a nodding acquaintance with Silverlight and Data Binding. Our final output will look something like this.Data
/* File : Node.cs */
02
using
System;
03
System.ComponentModel;
04
System.Collections.ObjectModel;
05
06
public
class
Node : INotifyPropertyChanged
07
{
08
private
String text;
09
10
ObservableCollection<Node> children;
11
12
event
PropertyChangedEventHandler PropertyChanged;
13
14
ObservableCollection<Node> Children
15
16
get
return
children; }
17
set
{ children = value; }
18
}
19
20
String Text
21
22
text; }
23
{ text = value; }
24
25
26
Node(String text)
27
28
Children =
new
ObservableCollection<Node>();
29
Text = text;
30
31
32
void
Add(Node node)
33
34
children.Add(node);
35
NotifyPropertyChanged(
"Children"
);
36
37
38
Delete(Node node)
39
40
children.Remove(node);
41
42
43
44
NotifyPropertyChanged(String info)
45
46
if
(PropertyChanged !=
null
)
47
PropertyChanged(
this
,
PropertyChangedEventArgs(info));
48
49
01
<!-- File : CSSLTreeViewCrudDragDrop.xaml -->
<
UserControl
xmlns:sdk
=
"http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
x:Class
"CSSLTreeViewCRUDDragDrop.TreeViewCrudDragDrop"
xmlns
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
"http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d
"http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc
"http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable
"d"
d:DesignHeight
"300"
d:DesignWidth
"400"
xmlns:toolkit
"clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
xmlns:mswindows
"clr-namespace:Microsoft.Windows;assembly=System.Windows.Controls.Toolkit"
>
UserControl.Resources
<!-- Template for Edit mode of TreeViewItem -->
sdk:HierarchicalDataTemplate
x:Key
"TreeViewMainEditTemplate"
ItemsSource
"{Binding Children}"
TextBox
Text
"{Binding Text,Mode=TwoWay}"
</
<!-- Template for Read mode for TreeViewItem -->
"TreeViewMainReadTemplate"
TextBlock
MouseRightButtonDown
"TreeViewMain_MouseRightButtonDown"
MouseRightButtonUp
"TreeViewMain_MouseRightButtonUp"
MouseLeftButtonDown
"TreeViewMain_MouseLeftButtonDown"
Grid
x:Name
"LayoutRoot"
Background
"White"
<!-- TreeViewDragDropTarget from Toolkit to add DragAndDrop feature -->
toolkit:TreeViewDragDropTarget
AllowDrop
"True"
<!-- Custom TreeView -->
sdk:TreeView
Name
"TreeViewMain"
ItemTemplate
"{StaticResource TreeViewMainReadTemplate}"
Width
Height
<!-- Context Menu -->
Canvas
Popup
"ContextMenu"
Visibility
"Collapsed"
Border
BorderThickness
"1"
BorderBrush
"Black"
StackPanel
HyperlinkButton
Content
"Add"
"AddButton"
Click
"AddButton_Click"
/>
"Edit"
"EditButton"
"EditButton_Click"
"Delete"
"DeleteButton"
"DeleteButton_Click"
50
51
52
53
54
Now let us have a sneak peek at the code behind for our UserControl.First, the Mouse Event Handlers. The MouseRightButtonUp event for a TreeViewItem does two things. It assigns that particular TreeViewItem’s Data Context as the selectedNode. Second, it shows up the ContextMenu. The selectedNode information is necessary as that is used as a reference to Edit the TreeViewItem, Add Children to the TreeViewItem or delete the TreeViewItem.The AddButton_Click event handler, creates a new Node and adds it as a children of the selecteNode.The EditButton_Click event handler, changes the Template of the selected TreeViewItem to Edit mode.The DeleteButton_Click event handler, first identifies the TreeViewItem associated with the selectedNode, finds its parent, and deletes the selectedNode from the Parent.
001
/* File : CSSLTreeViewCrudDragDrop.cs */
002
003
System.Collections.Generic;
004
System.Linq;
005
System.Windows;
006
System.Windows.Controls;
007
System.Windows.Input;
008
009
010
namespace
CSSLTreeViewCRUDDragDrop
011
012
013
partial
TreeViewCrudDragDrop : UserControl
014
015
ObservableCollection<Node> objectTree;
016
017
Node selectedNode;
018
019
List<Node> Items
020
021
022
023
objectTree.ToList<Node>();
024
025
026
027
objectTree =
ObservableCollection<Node>(value);
028
TreeViewMain.ItemsSource = objectTree;
029
030
031
032
TreeViewCrudDragDrop()
033
034
InitializeComponent();
035
036
037
038
039
TreeViewMain_MouseRightButtonDown(
object
sender, MouseButtonEventArgs e)
040
041
DisableEditForSelectedItem();
042
043
e.Handled =
true
;
044
045
046
TreeViewMain_MouseRightButtonUp(
047
048
049
050
(sender
is
TextBlock)
051
052
selectedNode = (Node)((sender
as
TextBlock).DataContext);
053
054
else
055
056
selectedNode =
057
058
059
ShowContextMenu(e);
060
061
062
TreeViewMain_MouseLeftButtonDown(
063
064
065
066
HideContextMenu();
067
068
069
AddButton_Click(
sender, RoutedEventArgs e)
070
071
Node newNode =
Node(
"New Node"
072
073
(selectedNode !=
074
075
selectedNode.Add(newNode);
076
077
078
079
(objectTree !=
080
081
objectTree.Add(newNode);
082
083
084
085
086
087
088
089
090
091
092
093
EditButton_Click(
094
095
EnalbleEditForSelectedItem();
096
097
TreeViewItem selectedTreeViewItem =
098
TreeViewExtensions.GetContainerFromItem(TreeViewMain, selectedNode);
099
100
101
102
103
DeleteButton_Click(
104
105
106
107
108
(selectedTreeViewItem !=
109
110
TreeViewItem selectedTreeViewItemParent =
111
TreeViewExtensions.GetParentTreeViewItem(selectedTreeViewItem);
112
113
(selectedTreeViewItemParent !=
114
115
Node seleactedParentNode = (Node)selectedTreeViewItemParent.DataContext;
116
seleactedParentNode.Delete(selectedNode);
117
118
119
120
objectTree.Remove(selectedNode);
121
122
123
124
125
126
127
ShowContextMenu(MouseButtonEventArgs e)
128
129
130
Point p = e.GetPosition(
131
ContextMenu.Visibility = Visibility.Visible;
132
ContextMenu.IsOpen =
133
ContextMenu.SetValue(Canvas.LeftProperty, (
double
)p.X);
134
ContextMenu.SetValue(Canvas.TopProperty, (
)p.Y);
135
136
137
HideContextMenu()
138
139
ContextMenu.Visibility = Visibility.Collapsed;
140
false
141
142
143
EnalbleEditForSelectedItem()
144
145
146
147
SetTemplateForSelectedItem(
148
149
150
151
DisableEditForSelectedItem()
152
153
154
155
156
157
158
159
160
SetTemplateForSelectedItem(String templateName)
161
162
HierarchicalDataTemplate hdt = (HierarchicalDataTemplate)Resources[templateName];
163
164
165
166
167
168
selectedTreeViewItem.HeaderTemplate = hdt;
169
170
171
So that completes it.
MichaelSnow : Silverlight Tip of the Day #3 – Mouse Right ClicksMSDN : DataBinding SilverlightCodeplex : Silverlight ToolkitMSDN : INotifyPropertyChanged Interface