Skip to content

Commit 5363017

Browse files
ES-975464 - Resolve the ReadMe issue in this sample repository
1 parent 1500679 commit 5363017

File tree

1 file changed

+118
-2
lines changed

1 file changed

+118
-2
lines changed

README.md

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,118 @@
1-
# How to convert event to command in MVVM pattern in wpf treeview?
2-
This repository describes how to convert event to command in MVVM pattern in wpf treeview
1+
# How to convert event to command in MVVM pattern in WPF TreeView
2+
3+
This repository describes how to convert event to command in MVVM pattern in [WPF TreeView](https://www.syncfusion.com/wpf-controls/treeview) (SfTreeView).
4+
5+
The `TreeView` event can be converted into commands using Behaviors. To achieve this, create a command in the `ViewModel` class and associate it to the `TreeView` event using Behaviors.
6+
7+
#### XAML
8+
9+
``` xml
10+
<syncfusion:SfTreeView
11+
x:Name="sfTreeView"
12+
Margin="20"
13+
BorderThickness="1"
14+
SelectionMode="Multiple"
15+
AutoExpandMode="AllNodes"
16+
BorderBrush="LightGray"
17+
ChildPropertyName="Models"
18+
ItemTemplateDataContextType="Node"
19+
ItemsSource="{Binding Items}">
20+
<syncfusion:SfTreeView.ItemTemplate>
21+
<DataTemplate>
22+
<Grid>
23+
<TextBlock FontSize="12" VerticalAlignment="Center" Text="{Binding Content.State}" />
24+
</Grid>
25+
</DataTemplate>
26+
</syncfusion:SfTreeView.ItemTemplate>
27+
<I:Interaction.Triggers>
28+
<I:EventTrigger EventName="SelectionChanged" >
29+
<cmd:EventToCommand Command="{Binding SelectionChangedCommand}" PassEventArgsToCommand="True" />
30+
</I:EventTrigger>
31+
</I:Interaction.Triggers>
32+
</syncfusion:SfTreeView>
33+
```
34+
35+
#### C#
36+
37+
``` csharp
38+
public class ViewModel : NotificationObject
39+
{
40+
public ObservableCollection<Model> Items { get; set; }
41+
42+
private ICommand selectionChangedCommand;
43+
44+
public ICommand SelectionChangedCommand
45+
{
46+
get
47+
{
48+
return selectionChangedCommand;
49+
}
50+
set
51+
{
52+
selectionChangedCommand = value;
53+
}
54+
}
55+
56+
public ViewModel()
57+
{
58+
Items = new ObservableCollection<Model>();
59+
60+
SelectionChangedCommand = new BaseCommand(OnSelectionChanged);
61+
62+
var country1 = new Model { State = "Australia" };
63+
var country2 = new Model { State = "Brazil" };
64+
var country3 = new Model { State = "China" };
65+
var country4 = new Model { State = "France" };
66+
67+
var aus_state1 = new Model { State = "New South Wales" };
68+
var aus_state2 = new Model { State = "Victoria" };
69+
var aus_state3 = new Model { State = "South Autralia" };
70+
var aus_state4 = new Model { State = "Western Australia" };
71+
72+
var brazil_state1 = new Model { State = "Parana" };
73+
var brazil_state2 = new Model { State = "Ceara" };
74+
var brazil_state3 = new Model { State = "Acre" };
75+
76+
var china_state1 = new Model { State = "Guangzhou" };
77+
var china_state2 = new Model { State = "Shanghai" };
78+
var china_state3 = new Model { State = "Beijing" };
79+
var china_state4 = new Model { State = "Shantou" };
80+
81+
var france_state1 = new Model { State = "Pays de la Loire" };
82+
var france_state2 = new Model { State = "Aquitaine" };
83+
var france_state3 = new Model { State = "Brittany" };
84+
var france_state4 = new Model { State = "Lorraine" };
85+
86+
country1.Models.Add(aus_state1);
87+
country1.Models.Add(aus_state2);
88+
country1.Models.Add(aus_state3);
89+
country1.Models.Add(aus_state4);
90+
91+
country2.Models.Add(brazil_state1);
92+
country2.Models.Add(brazil_state2);
93+
country2.Models.Add(brazil_state3);
94+
95+
country3.Models.Add(china_state1);
96+
country3.Models.Add(china_state2);
97+
country3.Models.Add(china_state3);
98+
country3.Models.Add(china_state4);
99+
100+
country4.Models.Add(france_state1);
101+
country4.Models.Add(france_state2);
102+
country4.Models.Add(france_state3);
103+
country4.Models.Add(france_state4);
104+
105+
Items.Add(country1);
106+
Items.Add(country2);
107+
Items.Add(country3);
108+
Items.Add(country4);
109+
}
110+
111+
private void OnSelectionChanged(object obj)
112+
{
113+
var args = obj as ItemSelectionChangedEventArgs;
114+
if (args.AddedItems.Count > 0)
115+
MessageBox.Show("Selected State: " + (args.AddedItems[0] as Model).State.ToString());
116+
}
117+
}
118+
```

0 commit comments

Comments
 (0)