Skip to content

Commit 2498d8f

Browse files
author
saravanan.ayyanar
committed
How to load data on demand using events in wpf treegrid
1 parent 1d4969a commit 2498d8f

17 files changed

+944
-0
lines changed

CS/App.ico

4.19 KB
Binary file not shown.

CS/App.xaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Application x:Class="OnDemandLoading.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources />
7+
</Application>

CS/App.xaml.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#region Copyright Syncfusion Inc. 2001 - 2015
2+
// Copyright Syncfusion Inc. 2001 - 2015. 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;
9+
using System.Collections.Generic;
10+
using System.Configuration;
11+
using System.Data;
12+
using System.Linq;
13+
using System.Windows;
14+
15+
namespace OnDemandLoading
16+
{
17+
/// <summary>
18+
/// Interaction logic for App.xaml
19+
/// </summary>
20+
public partial class App : Application
21+
{
22+
public App()
23+
{
24+
}
25+
}
26+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.Xaml.Behaviors;
2+
using Syncfusion.UI.Xaml.Grid;
3+
using Syncfusion.UI.Xaml.TreeGrid;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace OnDemandLoading
11+
{
12+
class RequestTreeItemsBehavior : Behavior<SfTreeGrid>
13+
{
14+
ViewModel viewModel;
15+
protected override void OnAttached()
16+
{
17+
base.OnAttached();
18+
viewModel = this.AssociatedObject.DataContext as ViewModel;
19+
this.AssociatedObject.RequestTreeItems += AssociatedObject_RequestTreeItems;
20+
21+
}
22+
23+
void AssociatedObject_RequestTreeItems(object sender, TreeGridRequestTreeItemsEventArgs args)
24+
{
25+
if (args.ParentItem == null)
26+
{
27+
//get the root list - get all employees who have no boss
28+
args.ChildItems = viewModel.EmployeeDetails.Where(x => x.ReportsTo == -1); //get all employees whose boss's id is -1 (no boss)
29+
}
30+
else //if ParentItem not null, then set args.ChildList to the child items for the given ParentItem.
31+
{ //get the children of the parent object
32+
EmployeeInfo emp = args.ParentItem as EmployeeInfo;
33+
if (emp != null)
34+
{
35+
//get all employees that report to the parent employee
36+
args.ChildItems = viewModel.GetReportees(emp.ID);
37+
}
38+
}
39+
}
40+
41+
protected override void OnDetaching()
42+
{
43+
base.OnDetaching();
44+
this.AssociatedObject.RequestTreeItems -= AssociatedObject_RequestTreeItems;
45+
}
46+
}
47+
}

CS/MainWindow.xaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<syncfusion:ChromelessWindow x:Class="OnDemandLoading.MainWindow"
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:OnDemandLoading"
5+
xmlns:interactivity="http://schemas.microsoft.com/xaml/behaviors"
6+
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
7+
Icon="App.ico"
8+
WindowStartupLocation="CenterScreen">
9+
<syncfusion:ChromelessWindow.DataContext>
10+
<local:ViewModel />
11+
</syncfusion:ChromelessWindow.DataContext>
12+
13+
<Grid>
14+
<syncfusion:SfTreeGrid Name="treeGrid"
15+
AutoGenerateColumns="False"
16+
ItemsSource="{Binding EmployeeDetails}">
17+
<interactivity:Interaction.Behaviors>
18+
<local:RequestTreeItemsBehavior />
19+
</interactivity:Interaction.Behaviors>
20+
21+
<syncfusion:SfTreeGrid.Columns>
22+
<syncfusion:TreeGridTextColumn HeaderText="First Name" MappingName="FirstName" />
23+
<syncfusion:TreeGridTextColumn HeaderText="Employee ID"
24+
MappingName="ID"
25+
TextAlignment="Left" />
26+
27+
<syncfusion:TreeGridTextColumn HeaderText="Last Name" MappingName="LastName" />
28+
<syncfusion:TreeGridTextColumn MappingName="Title" />
29+
<syncfusion:TreeGridCurrencyColumn MappingName="Salary" />
30+
<syncfusion:TreeGridTextColumn HeaderText="Reports To" MappingName="ReportsTo" />
31+
</syncfusion:SfTreeGrid.Columns>
32+
</syncfusion:SfTreeGrid>
33+
</Grid>
34+
</syncfusion:ChromelessWindow>

CS/MainWindow.xaml.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#region Copyright Syncfusion Inc. 2001 - 2016
2+
// Copyright Syncfusion Inc. 2001 - 2016. 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 Syncfusion.Windows.Shared;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
12+
using System.Text;
13+
using System.Windows;
14+
using System.Windows.Controls;
15+
using System.Windows.Data;
16+
using System.Windows.Documents;
17+
using System.Windows.Input;
18+
using System.Windows.Media;
19+
using System.Windows.Media.Imaging;
20+
using System.Windows.Navigation;
21+
using System.Windows.Shapes;
22+
23+
namespace OnDemandLoading
24+
{
25+
/// <summary>
26+
/// Interaction logic for MainWindow.xaml
27+
/// </summary>
28+
public partial class MainWindow : ChromelessWindow
29+
{
30+
public MainWindow()
31+
{
32+
InitializeComponent();
33+
}
34+
}
35+
}

CS/Model/EmployeeInfo.cs

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#region Copyright Syncfusion Inc. 2001 - 2016
2+
// Copyright Syncfusion Inc. 2001 - 2016. 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;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
using System.Text;
12+
using Syncfusion.Windows.Shared;
13+
using System.ComponentModel;
14+
using System.ComponentModel.DataAnnotations;
15+
16+
namespace OnDemandLoading
17+
{
18+
public class EmployeeInfo : NotificationObject//, IComparable<EmployeeInfo>
19+
{
20+
int _id;
21+
/// <summary>
22+
/// Gets or sets the ID.
23+
/// </summary>
24+
/// <value>The ID.</value>
25+
public int ID
26+
{
27+
get
28+
{
29+
return _id;
30+
}
31+
set
32+
{
33+
_id = value;
34+
RaisePropertyChanged("ID");
35+
}
36+
}
37+
string _firstName;
38+
39+
/// <summary>
40+
/// Gets or sets the first name.
41+
/// </summary>
42+
/// <value>The first name.</value>
43+
public string FirstName
44+
{
45+
get
46+
{
47+
return _firstName;
48+
}
49+
set
50+
{
51+
_firstName = value;
52+
RaisePropertyChanged("FirstName");
53+
}
54+
}
55+
string _lastName;
56+
57+
/// <summary>
58+
/// Gets or sets the last name.
59+
/// </summary>
60+
/// <value>The last name.</value>
61+
public string LastName
62+
{
63+
get
64+
{
65+
return _lastName;
66+
}
67+
set
68+
{
69+
_lastName = value;
70+
RaisePropertyChanged("LastName");
71+
}
72+
}
73+
string _department;
74+
75+
/// <summary>
76+
/// Gets or sets the department.
77+
/// </summary>
78+
/// <value>The department.</value>
79+
public string Department
80+
{
81+
get
82+
{
83+
return _department;
84+
}
85+
set
86+
{
87+
_department = value;
88+
RaisePropertyChanged("Department");
89+
}
90+
}
91+
private string _title;
92+
93+
/// <summary>
94+
/// Gets or sets the title.
95+
/// </summary>
96+
/// <value>The title.</value>
97+
public string Title
98+
{
99+
get
100+
{
101+
return _title;
102+
}
103+
set
104+
{
105+
_title = value;
106+
RaisePropertyChanged("Title");
107+
}
108+
}
109+
110+
double? _salary;
111+
/// <summary>
112+
/// Gets or sets the salary.
113+
/// </summary>
114+
/// <value>The salary.</value>
115+
public double? Salary
116+
{
117+
get
118+
{
119+
return _salary;
120+
}
121+
set
122+
{
123+
_salary = value;
124+
RaisePropertyChanged("Salary");
125+
}
126+
}
127+
128+
int _reportsTo;
129+
/// <summary>
130+
/// Gets or sets the reports to.
131+
/// </summary>
132+
/// <value>The reports to.</value>
133+
public int ReportsTo
134+
{
135+
get
136+
{
137+
return _reportsTo;
138+
}
139+
set
140+
{
141+
_reportsTo = value;
142+
RaisePropertyChanged("ReportsTo");
143+
}
144+
}
145+
146+
#region IComparable<Employee> Members
147+
148+
//public int CompareTo(EmployeeInfo other)
149+
//{
150+
// // return this.reportsTo - other.reportsTo;
151+
// if (other == null)
152+
// return -1;
153+
154+
// return this.ReportsTo.CompareTo(other.ReportsTo);
155+
//}
156+
157+
#endregion
158+
}
159+
}

0 commit comments

Comments
 (0)