-
Notifications
You must be signed in to change notification settings - Fork 914
Expand file tree
/
Copy pathSelectAllSelector.cs
More file actions
242 lines (190 loc) · 7.5 KB
/
Copy pathSelectAllSelector.cs
File metadata and controls
242 lines (190 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*************************************************************************************
Toolkit for WPF
Copyright (C) 2007-2024 Xceed Software Inc.
This program is provided to you under the terms of the XCEED SOFTWARE, INC.
COMMUNITY LICENSE AGREEMENT (for non-commercial use) as published at
https://github.com/xceedsoftware/wpftoolkit/blob/master/license.md
For more features, controls, and fast professional support,
pick up the Plus Edition at https://xceed.com/xceed-toolkit-plus-for-wpf/
Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
***********************************************************************************/
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
namespace Xceed.Wpf.Toolkit.Primitives
{
[TemplatePart( Name = PART_SelectAllSelectorItem, Type = typeof( SelectAllSelectorItem ) )]
public class SelectAllSelector : Selector
{
private const string PART_SelectAllSelectorItem = "PART_SelectAllSelectorItem";
#region Members
private SelectAllSelectorItem _selectAllSelecotrItem;
#endregion
#region Properties
#region AllItemsSelectedContent
public static readonly DependencyProperty AllItemsSelectedContentProperty = DependencyProperty.Register( "AllItemsSelectedContent", typeof( string ), typeof( SelectAllSelector )
, new UIPropertyMetadata( "All", OnAllItemsSelectedContentChanged ) );
public string AllItemsSelectedContent
{
get
{
return ( string )GetValue( AllItemsSelectedContentProperty );
}
set
{
SetValue( AllItemsSelectedContentProperty, value );
}
}
private static void OnAllItemsSelectedContentChanged( DependencyObject o, DependencyPropertyChangedEventArgs e )
{
var selectAllSelector = o as SelectAllSelector;
if( selectAllSelector != null )
selectAllSelector.OnAllItemsSelectedContentChanged( ( string )e.OldValue, ( string )e.NewValue );
}
protected virtual void OnAllItemsSelectedContentChanged( string oldValue, string newValue )
{
}
#endregion // SelectAllText
#region IsAllItemsSelectedContentActive
public static readonly DependencyProperty IsAllItemsSelectedContentActiveProperty = DependencyProperty.Register( "IsAllItemsSelectedContentActive", typeof( bool ), typeof( SelectAllSelector ), new UIPropertyMetadata( false, OnIsAllItemsSelectedContentActiveChanged ) );
public bool IsAllItemsSelectedContentActive
{
get
{
return ( bool )GetValue( IsAllItemsSelectedContentActiveProperty );
}
set
{
SetValue( IsAllItemsSelectedContentActiveProperty, value );
}
}
private static void OnIsAllItemsSelectedContentActiveChanged( DependencyObject o, DependencyPropertyChangedEventArgs e )
{
var selector = o as SelectAllSelector;
if( selector != null )
{
selector.OnIsAllItemsSelectedContentActiveChanged( ( bool )e.OldValue, ( bool )e.NewValue );
}
}
protected virtual void OnIsAllItemsSelectedContentActiveChanged( bool oldValue, bool newValue )
{
}
#endregion //IsAllItemsSelectedContentActive
#region IsSelectAllActive
public static readonly DependencyProperty IsSelectAllActiveProperty = DependencyProperty.Register( "IsSelectAllActive", typeof( bool ), typeof( SelectAllSelector ), new UIPropertyMetadata( false, OnIsSelectAllActiveChanged ) );
public bool IsSelectAllActive
{
get
{
return ( bool )GetValue( IsSelectAllActiveProperty );
}
set
{
SetValue( IsSelectAllActiveProperty, value );
}
}
private static void OnIsSelectAllActiveChanged( DependencyObject o, DependencyPropertyChangedEventArgs e )
{
var selector = o as SelectAllSelector;
if( selector != null )
selector.OnIsSelectAllActiveChanged( ( bool )e.OldValue, ( bool )e.NewValue );
}
protected virtual void OnIsSelectAllActiveChanged( bool oldValue, bool newValue )
{
if( newValue && ( this.Items.Count > 0 ) )
{
this.UpdateSelectAllSelectorItem();
}
}
#endregion //IsSelectAllActive
#region SelectAllContent
public static readonly DependencyProperty SelectAllContentProperty = DependencyProperty.Register( "SelectAllContent", typeof( object ), typeof( SelectAllSelector ), new UIPropertyMetadata( "Select All" ) );
public object SelectAllContent
{
get
{
return ( object )GetValue( SelectAllContentProperty );
}
set
{
SetValue( SelectAllContentProperty, value );
}
}
#endregion
#endregion
#region Overrides
protected override void OnSelectedItemsCollectionChanged( object sender, NotifyCollectionChangedEventArgs e )
{
base.OnSelectedItemsCollectionChanged( sender, e );
this.UpdateSelectAllSelectorItem();
}
protected override void OnItemsChanged( NotifyCollectionChangedEventArgs e )
{
base.OnItemsChanged( e );
this.UpdateSelectAllSelectorItem();
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_selectAllSelecotrItem = this.GetTemplateChild( PART_SelectAllSelectorItem ) as SelectAllSelectorItem;
}
#endregion
#region Public Methods
public void SelectAll()
{
var currentSelectedItems = new List<object>( this.SelectedItems.Cast<object>() );
var items = this.ItemsCollection.Cast<object>();
// Have a faster selection when there are more than 200 items.
this.UpdateSelectedItemsWithoutNotifications( items.ToList() );
// Raise SelectionChanged for new selected items.
var newSelectedItems = items.Except( currentSelectedItems );
foreach( var item in newSelectedItems )
{
this.OnItemSelectionChanged( new ItemSelectionChangedEventArgs( Selector.ItemSelectionChangedEvent, this, item, true ) );
if( this.Command != null )
{
this.Command.Execute( item );
}
}
}
public void UnSelectAll()
{
var currentSelectedItems = new List<object>( this.SelectedItems.Cast<object>() );
this.SelectedItems.Clear();
// Raise SelectionChanged for selected items.
foreach( var item in currentSelectedItems )
{
this.OnItemSelectionChanged( new ItemSelectionChangedEventArgs( Selector.ItemSelectionChangedEvent, this, item, false ) );
if( this.Command != null )
{
this.Command.Execute( item );
}
}
}
#endregion
#region Private Methods
private void UpdateSelectAllSelectorItem()
{
if( _selectAllSelecotrItem != null )
{
// All items are selected; select the SelectAll option.
if( this.Items.Count == this.SelectedItems.Count )
{
_selectAllSelecotrItem.ModifyCurrentSelection( true );
}
// Some items are selected; set the SelectAll option to null.
else if( this.SelectedItems.Count > 0 )
{
_selectAllSelecotrItem.ModifyCurrentSelection( null );
}
// No items are selected; unselect the SelectAll option.
else
{
_selectAllSelecotrItem.ModifyCurrentSelection( false );
}
}
}
#endregion
}
}