Skip to content

Commit 82c83b1

Browse files
Merge pull request #1 from susmitha-sundar/master
How to convert event to command in MVVM pattern in wpf treeview
2 parents edaabbc + 37c520f commit 82c83b1

File tree

18 files changed

+899
-0
lines changed

18 files changed

+899
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.168
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TreeViewEventToCommandDemo", "TreeViewEventToCommandDemo\TreeViewEventToCommandDemo.csproj", "{EF595223-272D-44BA-A5D7-24BB3340F628}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{EF595223-272D-44BA-A5D7-24BB3340F628}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{EF595223-272D-44BA-A5D7-24BB3340F628}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{EF595223-272D-44BA-A5D7-24BB3340F628}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{EF595223-272D-44BA-A5D7-24BB3340F628}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {90C8A490-DFB0-4461-8888-B7CBAA7B03FF}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
5+
</startup>
6+
<runtime>
7+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
8+
<dependentAssembly>
9+
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
10+
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
11+
</dependentAssembly>
12+
</assemblyBinding>
13+
</runtime>
14+
</configuration>
4.19 KB
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="TreeViewEventToCommandDemo.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:TreeViewEventToCommandDemo"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Data;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace TreeViewEventToCommandDemo
10+
{
11+
/// <summary>
12+
/// Interaction logic for App.xaml
13+
/// </summary>
14+
public partial class App : Application
15+
{
16+
}
17+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows.Input;
7+
8+
namespace TreeViewEventToCommandDemo
9+
{
10+
public class BaseCommand : ICommand
11+
{
12+
#region Fields
13+
14+
readonly Action<object> _execute;
15+
readonly Predicate<object> _canExecute;
16+
17+
#endregion // Fields
18+
19+
#region Constructors
20+
21+
/// <summary>
22+
/// Creates a new command that can always execute.
23+
/// </summary>
24+
/// <param name="execute">The execution logic.</param>
25+
public BaseCommand(Action<object> execute)
26+
: this(execute, null)
27+
{
28+
}
29+
30+
/// <summary>
31+
/// Creates a new command.
32+
/// </summary>
33+
/// <param name="execute">The execution logic.</param>
34+
/// <param name="canExecute">The execution status logic.</param>
35+
public BaseCommand(Action<object> execute, Predicate<object> canExecute)
36+
{
37+
if (execute == null)
38+
throw new ArgumentNullException("execute");
39+
40+
_execute = execute;
41+
_canExecute = canExecute;
42+
}
43+
44+
#endregion // Constructors
45+
46+
#region ICommand Members
47+
48+
public bool CanExecute(object parameter)
49+
{
50+
return _canExecute == null ? true : _canExecute(parameter);
51+
}
52+
53+
public event EventHandler CanExecuteChanged
54+
{
55+
add { CommandManager.RequerySuggested += value; }
56+
remove { CommandManager.RequerySuggested -= value; }
57+
}
58+
59+
public void Execute(object parameter)
60+
{
61+
_execute(parameter);
62+
}
63+
64+
#endregion // ICommand Members
65+
}
66+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Copyright (c) 2009-2017 Laurent Bugnion (GalaSoft), laurent@galasoft.ch
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"),
5+
to deal in the Software without restriction, including without limitation the
6+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7+
sell copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<Window x:Class="TreeViewEventToCommandDemo.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:TreeViewEventToCommandDemo"
7+
xmlns:I="http://schemas.microsoft.com/expression/2010/interactivity"
8+
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
9+
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
10+
Title="TreeView EventToCommand Demo"
11+
Width="400"
12+
Height="550"
13+
WindowStartupLocation="CenterScreen">
14+
<Window.DataContext>
15+
<local:ViewModel/>
16+
</Window.DataContext>
17+
<Grid>
18+
<syncfusion:SfTreeView
19+
x:Name="TreeView"
20+
Margin="20"
21+
BorderThickness="1"
22+
SelectionMode="Multiple"
23+
AutoExpandMode="AllNodes"
24+
BorderBrush="LightGray"
25+
ChildPropertyName="Models"
26+
ItemTemplateDataContextType="Node"
27+
ItemsSource="{Binding Items}">
28+
<syncfusion:SfTreeView.ItemTemplate>
29+
<DataTemplate>
30+
<Grid>
31+
<TextBlock FontSize="12" VerticalAlignment="Center" Text="{Binding Content.State}" />
32+
</Grid>
33+
</DataTemplate>
34+
</syncfusion:SfTreeView.ItemTemplate>
35+
<I:Interaction.Triggers>
36+
<I:EventTrigger EventName="SelectionChanged" >
37+
<cmd:EventToCommand Command="{Binding SelectionChangedCommand}" PassEventArgsToCommand="True" />
38+
</I:EventTrigger>
39+
</I:Interaction.Triggers>
40+
</syncfusion:SfTreeView>
41+
</Grid>
42+
</Window>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows;
7+
using System.Windows.Controls;
8+
using System.Windows.Data;
9+
using System.Windows.Documents;
10+
using System.Windows.Input;
11+
using System.Windows.Media;
12+
using System.Windows.Media.Imaging;
13+
using System.Windows.Navigation;
14+
using System.Windows.Shapes;
15+
16+
namespace TreeViewEventToCommandDemo
17+
{
18+
/// <summary>
19+
/// Interaction logic for MainWindow.xaml
20+
/// </summary>
21+
public partial class MainWindow : Window
22+
{
23+
public MainWindow()
24+
{
25+
InitializeComponent();
26+
}
27+
}
28+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#region Copyright Syncfusion Inc. 2001 - 2011
2+
// Copyright Syncfusion Inc. 2001 - 2011. All rights reserved.
3+
// Use of this code is subject to the terms of our license.
4+
// A copy of the current license can be obtained at any time by e-mailing
5+
// licensing@syncfusion.com. Any infringement will be prosecuted under
6+
// applicable laws.
7+
#endregion
8+
using System.Collections.ObjectModel;
9+
using System.Collections.Generic;
10+
using System.ComponentModel;
11+
using Syncfusion.Windows.Shared;
12+
using System.Windows.Media.Imaging;
13+
14+
namespace TreeViewEventToCommandDemo
15+
{
16+
public class Model : NotificationObject
17+
{
18+
public Model()
19+
{
20+
Models = new ObservableCollection<Model>();
21+
}
22+
23+
private ObservableCollection<Model> models;
24+
public ObservableCollection<Model> Models
25+
{
26+
get
27+
{
28+
return models;
29+
}
30+
31+
set
32+
{
33+
models = value;
34+
this.RaisePropertyChanged("Models");
35+
}
36+
}
37+
38+
#region TreeViewItemAdv Properties
39+
40+
private string state;
41+
/// <summary>
42+
/// Gets or sets a value indicating the Header of the TreeViewItemAdv.
43+
/// </summary>
44+
public string State
45+
{
46+
get
47+
{
48+
return state;
49+
}
50+
51+
set
52+
{
53+
state = value;
54+
this.RaisePropertyChanged("State");
55+
}
56+
}
57+
58+
59+
#endregion
60+
}
61+
}

0 commit comments

Comments
 (0)