Skip to content

Commit cac2dff

Browse files
author
saravanan.ayyanar
committed
How to change the checkbox column values based on selection in wpf treegrid
1 parent 4f444ee commit cac2dff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2873
-0
lines changed

CheckboxSelectionDemo/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
5+
</startup>
6+
</configuration>

CheckboxSelectionDemo/App.ico

4.19 KB
Binary file not shown.

CheckboxSelectionDemo/App.xaml

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

CheckboxSelectionDemo/App.xaml.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#region Copyright Syncfusion Inc. 2001-2018.
2+
// Copyright Syncfusion Inc. 2001-2018. 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.Threading.Tasks;
14+
using System.Windows;
15+
16+
namespace SelectionDemo
17+
{
18+
/// <summary>
19+
/// Interaction logic for App.xaml
20+
/// </summary>
21+
public partial class App : Application
22+
{
23+
}
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Syncfusion.UI.Xaml.ScrollAxis;
2+
using Syncfusion.UI.Xaml.TreeGrid;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.Windows.Interactivity;
9+
10+
namespace SelectionDemo
11+
{
12+
class SfTreeGridBehavior:Behavior<SfTreeGrid>
13+
{
14+
SfTreeGrid treegrid = null;
15+
protected override void OnAttached()
16+
{
17+
treegrid = this.AssociatedObject as SfTreeGrid;
18+
treegrid.Loaded += Treegrid_Loaded;
19+
base.OnAttached();
20+
}
21+
22+
private void Treegrid_Loaded(object sender, System.Windows.RoutedEventArgs e)
23+
{
24+
var selectedItem = (this.treegrid.DataContext as ViewModel).SelectedItem;
25+
var rowindex = this.treegrid.ResolveToRowIndex(selectedItem);
26+
var columnindex = this.treegrid.ResolveToStartColumnIndex();
27+
//Make the row in to available on the view.
28+
this.treegrid.ScrollInView(new RowColumnIndex(rowindex, columnindex));
29+
//Set the SelectedItem in SfTreeGrid.
30+
this.treegrid.SelectedItem = selectedItem;
31+
}
32+
}
33+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Syncfusion.UI.Xaml.Grid;
2+
using Syncfusion.UI.Xaml.TreeGrid;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.Windows.Controls;
9+
using System.Windows.Input;
10+
11+
namespace SelectionDemo
12+
{
13+
public static class Commands
14+
{
15+
static Commands()
16+
{
17+
CommandManager.RegisterClassCommandBinding(typeof(CheckBox), new CommandBinding(CheckAndUnCheck, OnCheckUnCheckCommand, OnCanExecuteCheckAndUnCheck));
18+
}
19+
20+
public static RoutedCommand CheckAndUnCheck = new RoutedCommand("CheckAndUnCheck", typeof(CheckBox));
21+
22+
private static void OnCheckUnCheckCommand(object sender, ExecutedRoutedEventArgs args)
23+
{
24+
var sfdatagrid = (args.Parameter as SfTreeGrid);
25+
var viewmodel = (sfdatagrid.DataContext as ViewModel);
26+
var checkbox = (sender as CheckBox).IsChecked;
27+
if (viewmodel != null)
28+
{
29+
if (checkbox == true)
30+
{
31+
sfdatagrid.SelectAll();
32+
foreach (var collection in viewmodel.EmployeeInfo)
33+
{
34+
if (collection.IsSelected == false)
35+
collection.IsSelected = true;
36+
}
37+
}
38+
else if (checkbox == false)
39+
{
40+
sfdatagrid.ClearSelections(false);
41+
foreach (var collection in viewmodel.EmployeeInfo)
42+
{
43+
if (collection.IsSelected == true)
44+
collection.IsSelected = false;
45+
}
46+
}
47+
}
48+
}
49+
50+
private static void OnCanExecuteCheckAndUnCheck(object sender, CanExecuteRoutedEventArgs args)
51+
{
52+
args.CanExecute = true;
53+
}
54+
}
55+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#region Copyright Syncfusion Inc. 2001-2018.
2+
// Copyright Syncfusion Inc. 2001-2018. 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.UI.Xaml.Grid;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Globalization;
12+
using System.Linq;
13+
using System.Text;
14+
using System.Threading.Tasks;
15+
using System.Windows.Data;
16+
17+
namespace SelectionDemo
18+
{
19+
internal class SelectionModeConverter : IValueConverter
20+
{
21+
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo info)
22+
{
23+
int? index = value as int?;
24+
switch (index)
25+
{
26+
case 0:
27+
return GridSelectionMode.Single;
28+
case 1:
29+
return GridSelectionMode.Multiple;
30+
case 2:
31+
return GridSelectionMode.Extended;
32+
case 3:
33+
return GridSelectionMode.None;
34+
default:
35+
return null;
36+
}
37+
}
38+
39+
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo info)
40+
{
41+
throw new NotImplementedException();
42+
}
43+
}
44+
45+
internal class NavigationModeConverter:IValueConverter
46+
{
47+
48+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
49+
{
50+
int? index = value as int?;
51+
switch (index)
52+
{
53+
case 0:
54+
return NavigationMode.Row;
55+
case 1:
56+
return NavigationMode.Cell;
57+
58+
default:
59+
return null;
60+
}
61+
}
62+
63+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
64+
{
65+
throw new NotImplementedException();
66+
}
67+
}
68+
69+
public class SelectionImageConverter : IValueConverter
70+
{
71+
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
72+
{
73+
if (value != null && value is EmployeeInfo)
74+
{
75+
var name = value as EmployeeInfo;
76+
return "Images/" + name.FirstName + ".png";
77+
}
78+
return null;
79+
}
80+
81+
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
82+
{
83+
throw new NotImplementedException();
84+
}
85+
}
86+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using Syncfusion.UI.Xaml.Grid;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.Windows;
9+
using System.Windows.Controls;
10+
using System.Windows.Data;
11+
using System.Windows.Documents;
12+
using System.Windows.Input;
13+
using System.Windows.Media;
14+
using System.Windows.Media.Imaging;
15+
using System.Windows.Navigation;
16+
using System.Windows.Shapes;
17+
using Syncfusion.UI.Xaml.Grid.Helpers;
18+
using System.Collections.ObjectModel;
19+
using Syncfusion.UI.Xaml.Utility;
20+
using System.Windows.Threading;
21+
using Syncfusion.UI.Xaml.ScrollAxis;
22+
using Syncfusion.Data;
23+
using Syncfusion.UI.Xaml.Grid.Cells;
24+
using Syncfusion.Windows.Shared;
25+
using Syncfusion.UI.Xaml.TreeGrid;
26+
27+
namespace SelectionDemo
28+
{
29+
30+
public class RowSelectionController : TreeGridRowSelectionController
31+
{
32+
public RowSelectionController(SfTreeGrid treeGrid)
33+
: base(treeGrid)
34+
{ }
35+
36+
protected override void ProcessPointerReleased(MouseButtonEventArgs args, RowColumnIndex rowColumnIndex)
37+
{
38+
base.ProcessPointerReleased(args, rowColumnIndex);
39+
CheckBoxSelection(rowColumnIndex);
40+
args.Handled = true;
41+
this.TreeGrid.Focus();
42+
}
43+
44+
protected override void ProcessKeyDown(KeyEventArgs args)
45+
{
46+
base.ProcessKeyDown(args);
47+
if (args.Key == Key.Space)
48+
CheckBoxSelection(this.CurrentCellManager.CurrentRowColumnIndex);
49+
}
50+
51+
private void CheckBoxSelection(RowColumnIndex rowcolumnIndex)
52+
{
53+
var selectedrow = this.SelectedRows.FindAll(item => item.RowIndex == rowcolumnIndex.RowIndex);
54+
if (selectedrow.Count == 0)
55+
{
56+
var row = this.TreeGrid.GetNodeAtRowIndex(rowcolumnIndex.RowIndex).Item;
57+
(row as EmployeeInfo).IsSelected = false;
58+
}
59+
else
60+
(selectedrow[0].RowData as EmployeeInfo).IsSelected = true;
61+
var collectioncount = (this.TreeGrid.DataContext as ViewModel).EmployeeInfo.Count;
62+
var selecteditemcount = this.TreeGrid.SelectedItems.Count;
63+
if (selecteditemcount == collectioncount)
64+
(this.TreeGrid.DataContext as ViewModel).IsSelectAll = true;
65+
else if (selecteditemcount == 0)
66+
(this.TreeGrid.DataContext as ViewModel).IsSelectAll = false;
67+
else
68+
(this.TreeGrid.DataContext as ViewModel).IsSelectAll = null;
69+
}
70+
}
71+
}
45.3 KB
Loading
47.4 KB
Loading

0 commit comments

Comments
 (0)