Skip to content

Commit de05db8

Browse files
Merge pull request #1 from vijayarasan/main
How to edit an item in WinUI TreeView (SfTreeView)?
2 parents 0748ab4 + e8f540d commit de05db8

27 files changed

+1019
-2
lines changed

Editing.gif

206 KB
Loading

README.md

Lines changed: 236 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,236 @@
1-
# How to edit an item in winui treeview?
2-
This example describes how to edit an item in winui treeview.
1+
# How to edit an item in WinUI Treeview?
2+
3+
## About the sample
4+
5+
This example describes how to edit an item in WinUI Treeview.
6+
7+
[WinUI TreeView](https://www.syncfusion.com/winui-controls/treeview) (SfTreeView) provides support for editing and it can be enabled or disabled by using [SfTreeView.AllowEditing](https://help.syncfusion.com/cr/winui/Syncfusion.UI.Xaml.TreeView.SfTreeView.html#Syncfusion_UI_Xaml_TreeView_SfTreeView_AllowEditing) property. Editing can be triggered for the nodes in TreeView by pressing **F2** key only.
8+
9+
The changes made by editing a node will be committed only when user move selection to next node or by pressing **Enter** key.
10+
11+
It is necessary to define [EditTemplate](https://help.syncfusion.com/cr/winui/Syncfusion.UI.Xaml.TreeView.SfTreeView.html#Syncfusion_UI_Xaml_TreeView_SfTreeView_EditTemplate) / [EditTemplateSelector](https://help.syncfusion.com/cr/winui/Syncfusion.UI.Xaml.TreeView.SfTreeView.html#Syncfusion_UI_Xaml_TreeView_SfTreeView_EditTemplateSelector) for bound mode, to enable editing. For UnboundMode, textbox will be loaded in edit mode by default.
12+
13+
``` XML
14+
15+
<treeView:SfTreeView x:Name="treeView"
16+
Width="400"
17+
Height="500"
18+
AllowEditing="True"
19+
AutoExpandMode="AllNodes"
20+
ChildPropertyName="Childs"
21+
BorderBrush="LightGray"
22+
IsAnimationEnabled="True"
23+
BorderThickness="1"
24+
FullRowSelect="True"
25+
ItemsSource="{Binding Nodes1}">
26+
<treeView:SfTreeView.ItemTemplate>
27+
<DataTemplate>
28+
<StackPanel Orientation="Horizontal">
29+
<ContentPresenter Width="20"
30+
Height="20"
31+
HorizontalAlignment="Stretch"
32+
VerticalAlignment="Center"
33+
ContentTemplate="{Binding ImageTemplate}" />
34+
<TextBlock
35+
Margin="5"
36+
VerticalAlignment="Center"
37+
Text="{Binding Header}" />
38+
</StackPanel>
39+
</DataTemplate>
40+
</treeView:SfTreeView.ItemTemplate>
41+
<treeView:SfTreeView.EditTemplate>
42+
<DataTemplate>
43+
<TextBox VerticalAlignment="Center"
44+
Height="{Binding Path=ItemHeight,ElementName=treeView}"
45+
BorderThickness="1"
46+
Text="{Binding Header,Mode=TwoWay}"/>
47+
</DataTemplate>
48+
</treeView:SfTreeView.EditTemplate>
49+
</treeView:SfTreeView>
50+
51+
```
52+
53+
![Shows the editing in SfTreeView](Editing.gif)
54+
55+
## Programmatic Editing
56+
57+
### Begin the editing
58+
59+
Editing in TreeView can be triggered programmatically by calling the [BeginEdit](https://help.syncfusion.com/cr/winui/Syncfusion.UI.Xaml.TreeView.SfTreeView.html#Syncfusion_UI_Xaml_TreeView_SfTreeView_BeginEdit_Syncfusion_UI_Xaml_TreeView_TreeViewNode_) method.
60+
61+
``` C#
62+
63+
this.treeView.Loaded += OnLoaded;
64+
65+
private void OnLoaded(object sender, RoutedEventArgs e)
66+
{
67+
this.treeView.BeginEdit(this.treeView.Nodes[0]);
68+
}
69+
70+
```
71+
72+
#### NOTE: [CurrentItem](https://help.syncfusion.com/cr/winui/Syncfusion.UI.Xaml.TreeView.SfTreeView.html#Syncfusion_UI_Xaml_TreeView_SfTreeView_CurrentItem) is set to the node when the BeginEdit is called.
73+
74+
### End the editing
75+
76+
[EndEdit](https://help.syncfusion.com/cr/winui/Syncfusion.UI.Xaml.TreeView.SfTreeView.html#Syncfusion_UI_Xaml_TreeView_SfTreeView_EndEdit_Syncfusion_UI_Xaml_TreeView_TreeViewNode_) method is used to programmatically end the editing for specific node.
77+
78+
``` C#
79+
80+
this.treeView.Loaded += OnLoaded;
81+
82+
private void OnLoaded(object sender, RoutedEventArgs e)
83+
{
84+
this.treeView.EndEdit(this.treeView.Nodes[0]);
85+
}
86+
87+
```
88+
89+
## Cancel editing for specific node
90+
91+
Editing for a specific node in TreeView can be cancelled by handling the [ItemBeginEdit](https://help.syncfusion.com/cr/winui/Syncfusion.UI.Xaml.TreeView.SfTreeView.html#Syncfusion_UI_Xaml_TreeView_SfTreeView_ItemBeginEdit) event.
92+
93+
``` C#
94+
95+
treeView.ItemBeginEdit += OnItemBeginEdit;
96+
97+
private void OnItemBeginEdit(object sender, Syncfusion.UI.Xaml.TreeView.TreeViewItemBeginEditEventArgs e)
98+
{
99+
if ((e.Node.Content as Folder).FileName == "Documents")
100+
e.Cancel = true;
101+
}
102+
103+
```
104+
105+
## Revert the edited changes while pressing Escape key
106+
107+
WinUI TreeView does not have support to rollback the changes when pressing the **ESC** key while editing the TreeView node. But it supports to rollback the changes when underlying data object implements the [IEditableObject](https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.ieditableobject?view=net-6.0) interface.
108+
109+
The user can take a backup of existing data of a node in the [BeginEdit](https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.ieditableobject.beginedit?view=net-6.0) method and can change the existing data to the current data in the [CancelEdit](https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.ieditableobject.canceledit?view=net-6.0) method to rollback the changes.
110+
111+
The below code snippet explains the simple implementation of IEditableObject interface to rollback the changes.
112+
113+
``` C#
114+
115+
public class EditingModel : INotifyPropertyChanged, IEditableObject
116+
{
117+
#region Fields
118+
119+
private string name;
120+
internal EditingModel backUpData;
121+
private EditingModel currentData;
122+
123+
private string header = string.Empty;
124+
private bool isexpanded = true;
125+
private DataTemplate imageTemplate;
126+
private ObservableCollection<EditingModel> childs = null;
127+
128+
#endregion
129+
130+
#region Constructor
131+
132+
public EditingModel()
133+
{
134+
135+
}
136+
137+
public EditingModel(string name):base()
138+
{
139+
Childs = new ObservableCollection<EditingModel>();
140+
this.currentData = new EditingModel();
141+
this.currentData.name = name;
142+
}
143+
144+
#endregion
145+
146+
#region Properties
147+
public string Header
148+
{
149+
get
150+
{
151+
return currentData.name;
152+
}
153+
set
154+
{
155+
currentData.name = value;
156+
this.RaisePropertyChanged("Header");
157+
}
158+
}
159+
160+
public bool IsExpanded
161+
{
162+
get
163+
{
164+
return isexpanded;
165+
}
166+
set
167+
{
168+
isexpanded = value;
169+
this.RaisePropertyChanged("IsExpanded");
170+
}
171+
}
172+
173+
public DataTemplate ImageTemplate
174+
{
175+
get { return imageTemplate; }
176+
set { imageTemplate = value; }
177+
}
178+
179+
public ObservableCollection<EditingModel> Childs
180+
{
181+
get
182+
{
183+
return childs;
184+
}
185+
set
186+
{
187+
childs = value;
188+
this.RaisePropertyChanged("Childs");
189+
}
190+
}
191+
192+
#endregion
193+
194+
#region INotifyPropertyChanged
195+
196+
public event PropertyChangedEventHandler PropertyChanged;
197+
198+
public void RaisePropertyChanged(string _PropertyName)
199+
{
200+
if (PropertyChanged != null)
201+
{
202+
PropertyChanged(this, new PropertyChangedEventArgs(_PropertyName));
203+
}
204+
}
205+
206+
#endregion
207+
208+
#region IEditableObject
209+
210+
public void BeginEdit()
211+
{
212+
backUpData = new EditingModel();
213+
backUpData.name = this.currentData.name;
214+
}
215+
216+
public void EndEdit()
217+
{
218+
219+
}
220+
221+
public void CancelEdit()
222+
{
223+
this.currentData = backUpData;
224+
}
225+
226+
#endregion
227+
}
228+
229+
```
230+
231+
![Shows the revert the edited changes while pressing Escape key in SfTreeView](RevertChanges.gif)
232+
233+
Take a moment to peruse the [WinUI TreeView - Editing](https://help.syncfusion.com/winui/treeview/editing) documentation, where you can find about editing with code examples.
234+
235+
## Requirements to run the demo
236+
Visual Studio 2019 and above versions

RevertChanges.gif

295 KB
Loading
432 Bytes
Loading
5.25 KB
Loading
1.71 KB
Loading
637 Bytes
Loading
283 Bytes
Loading
456 Bytes
Loading
2.05 KB
Loading

0 commit comments

Comments
 (0)