-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ItemsControl.cs
4069 lines (3585 loc) · 163 KB
/
ItemsControl.cs
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Threading;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows;
using System.Windows.Media;
using System.Windows.Markup;
using System.Windows.Input;
using System.Windows.Automation.Peers;
using MS.Utility;
using MS.Internal;
using MS.Internal.Controls;
using MS.Internal.Data;
using MS.Internal.Hashing.PresentationFramework; // HashHelper
using MS.Internal.KnownBoxes;
using MS.Internal.PresentationFramework;
using MS.Internal.Utility;
namespace System.Windows.Controls
{
/// <summary>
/// The base class for all controls that have multiple children.
/// </summary>
/// <remarks>
/// ItemsControl adds Items, ItemTemplate, and Part features to a Control.
/// </remarks>
// Needs DefaultEvent("ItemsChanged") ?
[DefaultEvent("OnItemsChanged"), DefaultProperty("Items")]
[ContentProperty("Items")]
[StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(FrameworkElement))]
[Localizability(LocalizationCategory.None, Readability = Readability.Unreadable)] // cannot be read & localized as string
public class ItemsControl : Control, IAddChild, IGeneratorHost, IContainItemStorage
{
#region Constructors
/// <summary>
/// Default ItemsControl constructor
/// </summary>
/// <remarks>
/// Automatic determination of current Dispatcher. Use alternative constructor
/// that accepts a Dispatcher for best performance.
/// </remarks>
public ItemsControl() : base()
{
ShouldCoerceCacheSizeField.SetValue(this, true);
this.CoerceValue(VirtualizingPanel.CacheLengthUnitProperty);
}
static ItemsControl()
{
// Define default style in code instead of in theme files.
DefaultStyleKeyProperty.OverrideMetadata(typeof(ItemsControl), new FrameworkPropertyMetadata(typeof(ItemsControl)));
_dType = DependencyObjectType.FromSystemTypeInternal(typeof(ItemsControl));
EventManager.RegisterClassHandler(typeof(ItemsControl), Keyboard.GotKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(OnGotFocus));
VirtualizingStackPanel.ScrollUnitProperty.OverrideMetadata(typeof(ItemsControl), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnScrollingModeChanged), new CoerceValueCallback(CoerceScrollingMode)));
VirtualizingPanel.CacheLengthProperty.OverrideMetadata(typeof(ItemsControl), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnCacheSizeChanged)));
VirtualizingPanel.CacheLengthUnitProperty.OverrideMetadata(typeof(ItemsControl), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnCacheSizeChanged), new CoerceValueCallback(CoerceVirtualizationCacheLengthUnit)));
}
private static void OnScrollingModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ShouldCoerceScrollUnitField.SetValue(d, true);
d.CoerceValue(VirtualizingStackPanel.ScrollUnitProperty);
}
private static object CoerceScrollingMode(DependencyObject d, object baseValue)
{
if (ShouldCoerceScrollUnitField.GetValue(d))
{
ShouldCoerceScrollUnitField.SetValue(d, false);
BaseValueSource baseValueSource = DependencyPropertyHelper.GetValueSource(d, VirtualizingStackPanel.ScrollUnitProperty).BaseValueSource;
if (((ItemsControl)d).IsGrouping && baseValueSource == BaseValueSource.Default)
{
return ScrollUnit.Pixel;
}
}
return baseValue;
}
private static void OnCacheSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ShouldCoerceCacheSizeField.SetValue(d, true);
d.CoerceValue(e.Property);
}
//default VCLU will be Item for the flat non-grouping case
private static object CoerceVirtualizationCacheLengthUnit(DependencyObject d, object baseValue)
{
if (ShouldCoerceCacheSizeField.GetValue(d))
{
ShouldCoerceCacheSizeField.SetValue(d, false);
BaseValueSource baseValueSource = DependencyPropertyHelper.GetValueSource(d, VirtualizingStackPanel.CacheLengthUnitProperty).BaseValueSource;
if ( !((ItemsControl)d).IsGrouping && !(d is TreeView) && baseValueSource == BaseValueSource.Default )
{
return VirtualizationCacheLengthUnit.Item;
}
}
return baseValue;
}
private void CreateItemCollectionAndGenerator()
{
_items = new ItemCollection(this);
// ItemInfos must get adjusted before the generator's change handler is called,
// so that any new ItemInfos arising from the generator don't get adjusted by mistake
// (see Win8 690623).
((INotifyCollectionChanged)_items).CollectionChanged += new NotifyCollectionChangedEventHandler(OnItemCollectionChanged1);
// the generator must attach its collection change handler before
// the control itself, so that the generator is up-to-date by the
// time the control tries to use it (bug 892806 et al.)
_itemContainerGenerator = new ItemContainerGenerator(this);
_itemContainerGenerator.ChangeAlternationCount();
((INotifyCollectionChanged)_items).CollectionChanged += new NotifyCollectionChangedEventHandler(OnItemCollectionChanged2);
if (IsInitPending)
{
_items.BeginInit();
}
else if (IsInitialized)
{
_items.BeginInit();
_items.EndInit();
}
((INotifyCollectionChanged)_groupStyle).CollectionChanged += new NotifyCollectionChangedEventHandler(OnGroupStyleChanged);
}
#endregion
#region Properties
/// <summary>
/// Items is the collection of data that is used to generate the content
/// of this control.
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Bindable(true), CustomCategory("Content")]
public ItemCollection Items
{
get
{
if (_items == null)
{
CreateItemCollectionAndGenerator();
}
return _items;
}
}
/// <summary>
/// This method is used by TypeDescriptor to determine if this property should
/// be serialized.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeItems()
{
return HasItems;
}
/// <summary>
/// The DependencyProperty for the ItemsSource property.
/// Flags: None
/// Default Value: null
/// </summary>
[CommonDependencyProperty]
public static readonly DependencyProperty ItemsSourceProperty
= DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(ItemsControl),
new FrameworkPropertyMetadata((IEnumerable)null,
new PropertyChangedCallback(OnItemsSourceChanged)));
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ItemsControl ic = (ItemsControl) d;
IEnumerable oldValue = (IEnumerable)e.OldValue;
IEnumerable newValue = (IEnumerable)e.NewValue;
((IContainItemStorage)ic).Clear();
BindingExpressionBase beb = BindingOperations.GetBindingExpressionBase(d, ItemsSourceProperty);
if (beb != null)
{
// ItemsSource is data-bound. Always go to ItemsSource mode.
// Also, extract the source item, to supply as context to the
// CollectionRegistering event
ic.Items.SetItemsSource(newValue, (object x)=>beb.GetSourceItem(x) );
}
else if (e.NewValue != null)
{
// ItemsSource is non-null, but not data-bound. Go to ItemsSource mode
ic.Items.SetItemsSource(newValue);
}
else
{
// ItemsSource is explicitly null. Return to normal mode.
ic.Items.ClearItemsSource();
}
ic.OnItemsSourceChanged(oldValue, newValue);
}
/// <summary>
/// Called when the value of ItemsSource changes.
/// </summary>
protected virtual void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
}
/// <summary>
/// ItemsSource specifies a collection used to generate the content of
/// this control. This provides a simple way to use exactly one collection
/// as the source of content for this control.
/// </summary>
/// <remarks>
/// Any existing contents of the Items collection is replaced when this
/// property is set. The Items collection will be made ReadOnly and FixedSize.
/// When ItemsSource is in use, setting this property to null will remove
/// the collection and restore use to Items (which will be an empty ItemCollection).
/// When ItemsSource is not in use, the value of this property is null, and
/// setting it to null has no effect.
/// </remarks>
[Bindable(true), CustomCategory("Content")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IEnumerable ItemsSource
{
get { return Items.ItemsSource; }
set
{
if (value == null)
{
ClearValue(ItemsSourceProperty);
}
else
{
SetValue(ItemsSourceProperty, value);
}
}
}
/// <summary>
/// The ItemContainerGenerator associated with this control
/// </summary>
[Bindable(false), Browsable(false), EditorBrowsable(EditorBrowsableState.Advanced)]
public ItemContainerGenerator ItemContainerGenerator
{
get
{
if (_itemContainerGenerator == null)
{
CreateItemCollectionAndGenerator();
}
return _itemContainerGenerator;
}
}
/// <summary>
/// Returns enumerator to logical children
/// </summary>
protected internal override IEnumerator LogicalChildren
{
get
{
if (!HasItems)
{
return EmptyEnumerator.Instance;
}
// Items in direct-mode of ItemCollection are the only model children.
// note: the enumerator walks the ItemCollection.InnerList as-is,
// no flattening of any content on model children level!
return this.Items.LogicalChildren;
}
}
// this is called before the generator's change handler
private void OnItemCollectionChanged1(object sender, NotifyCollectionChangedEventArgs e)
{
AdjustItemInfoOverride(e);
}
// this is called after the generator's change handler
private void OnItemCollectionChanged2(object sender, NotifyCollectionChangedEventArgs e)
{
SetValue(HasItemsPropertyKey, (_items != null) && !_items.IsEmpty);
// If the focused item is removed, drop our reference to it.
if (_focusedInfo != null && _focusedInfo.Index < 0)
{
_focusedInfo = null;
}
// on Reset, discard item storage
if (e.Action == NotifyCollectionChangedAction.Reset)
{
((IContainItemStorage)this).Clear();
}
OnItemsChanged(e);
}
/// <summary>
/// This method is invoked when the Items property changes.
/// </summary>
protected virtual void OnItemsChanged(NotifyCollectionChangedEventArgs e)
{
}
/// <summary>
/// Adjust ItemInfos when the Items property changes.
/// </summary>
internal virtual void AdjustItemInfoOverride(NotifyCollectionChangedEventArgs e)
{
AdjustItemInfo(e, _focusedInfo);
}
/// <summary>
/// The key needed set a read-only property.
/// </summary>
internal static readonly DependencyPropertyKey HasItemsPropertyKey =
DependencyProperty.RegisterReadOnly(
"HasItems",
typeof(bool),
typeof(ItemsControl),
new FrameworkPropertyMetadata(BooleanBoxes.FalseBox, OnVisualStatePropertyChanged));
/// <summary>
/// The DependencyProperty for the HasItems property.
/// Flags: None
/// Other: Read-Only
/// Default Value: false
/// </summary>
public static readonly DependencyProperty HasItemsProperty =
HasItemsPropertyKey.DependencyProperty;
/// <summary>
/// True if Items.Count > 0, false otherwise.
/// </summary>
[Bindable(false), Browsable(false)]
public bool HasItems
{
get { return (bool) GetValue(HasItemsProperty); }
}
/// <summary>
/// The DependencyProperty for the DisplayMemberPath property.
/// Flags: none
/// Default Value: string.Empty
/// </summary>
public static readonly DependencyProperty DisplayMemberPathProperty =
DependencyProperty.Register(
"DisplayMemberPath",
typeof(string),
typeof(ItemsControl),
new FrameworkPropertyMetadata(
string.Empty,
new PropertyChangedCallback(OnDisplayMemberPathChanged)));
/// <summary>
/// DisplayMemberPath is a simple way to define a default template
/// that describes how to convert Items into UI elements by using
/// the specified path.
/// </summary>
[Bindable(true), CustomCategory("Content")]
public string DisplayMemberPath
{
get { return (string) GetValue(DisplayMemberPathProperty); }
set { SetValue(DisplayMemberPathProperty, value); }
}
/// <summary>
/// Called when DisplayMemberPathProperty is invalidated on "d."
/// </summary>
private static void OnDisplayMemberPathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ItemsControl ctrl = (ItemsControl) d;
ctrl.OnDisplayMemberPathChanged((string)e.OldValue, (string)e.NewValue);
ctrl.UpdateDisplayMemberTemplateSelector();
}
// DisplayMemberPath and ItemStringFormat use the ItemTemplateSelector property
// to achieve the desired result. When either of these properties change,
// update the ItemTemplateSelector property here.
private void UpdateDisplayMemberTemplateSelector()
{
string displayMemberPath = DisplayMemberPath;
string itemStringFormat = ItemStringFormat;
if (!String.IsNullOrEmpty(displayMemberPath) || !String.IsNullOrEmpty(itemStringFormat))
{
// One or both of DisplayMemberPath and ItemStringFormat are desired.
// Set ItemTemplateSelector to an appropriate object, provided that
// this doesn't conflict with the user's own setting.
DataTemplateSelector itemTemplateSelector = ItemTemplateSelector;
if (itemTemplateSelector != null && !(itemTemplateSelector is DisplayMemberTemplateSelector))
{
// if ITS was actually set to something besides a DisplayMember selector,
// it's an error to overwrite it with a DisplayMember selector
// unless ITS came from a style and DMP is local
if (ReadLocalValue(ItemTemplateSelectorProperty) != DependencyProperty.UnsetValue ||
ReadLocalValue(DisplayMemberPathProperty) == DependencyProperty.UnsetValue)
{
throw new InvalidOperationException(SR.DisplayMemberPathAndItemTemplateSelectorDefined);
}
}
// now set the ItemTemplateSelector to use the new DisplayMemberPath and ItemStringFormat
ItemTemplateSelector = new DisplayMemberTemplateSelector(DisplayMemberPath, ItemStringFormat);
}
else
{
// Neither property is desired. Clear the ItemTemplateSelector if
// we had set it earlier.
if (ItemTemplateSelector is DisplayMemberTemplateSelector)
{
ClearValue(ItemTemplateSelectorProperty);
}
}
}
/// <summary>
/// This method is invoked when the DisplayMemberPath property changes.
/// </summary>
/// <param name="oldDisplayMemberPath">The old value of the DisplayMemberPath property.</param>
/// <param name="newDisplayMemberPath">The new value of the DisplayMemberPath property.</param>
protected virtual void OnDisplayMemberPathChanged(string oldDisplayMemberPath, string newDisplayMemberPath)
{
}
/// <summary>
/// The DependencyProperty for the ItemTemplate property.
/// Flags: none
/// Default Value: null
/// </summary>
[CommonDependencyProperty]
public static readonly DependencyProperty ItemTemplateProperty =
DependencyProperty.Register(
"ItemTemplate",
typeof(DataTemplate),
typeof(ItemsControl),
new FrameworkPropertyMetadata(
(DataTemplate) null,
new PropertyChangedCallback(OnItemTemplateChanged)));
/// <summary>
/// ItemTemplate is the template used to display each item.
/// </summary>
[Bindable(true), CustomCategory("Content")]
public DataTemplate ItemTemplate
{
get { return (DataTemplate) GetValue(ItemTemplateProperty); }
set { SetValue(ItemTemplateProperty, value); }
}
/// <summary>
/// Called when ItemTemplateProperty is invalidated on "d."
/// </summary>
/// <param name="d">The object on which the property was invalidated.</param>
/// <param name="e">EventArgs that contains the old and new values for this property</param>
private static void OnItemTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ItemsControl) d).OnItemTemplateChanged((DataTemplate) e.OldValue, (DataTemplate) e.NewValue);
}
/// <summary>
/// This method is invoked when the ItemTemplate property changes.
/// </summary>
/// <param name="oldItemTemplate">The old value of the ItemTemplate property.</param>
/// <param name="newItemTemplate">The new value of the ItemTemplate property.</param>
protected virtual void OnItemTemplateChanged(DataTemplate oldItemTemplate, DataTemplate newItemTemplate)
{
CheckTemplateSource();
if (_itemContainerGenerator != null)
{
_itemContainerGenerator.Refresh();
}
}
/// <summary>
/// The DependencyProperty for the ItemTemplateSelector property.
/// Flags: none
/// Default Value: null
/// </summary>
[CommonDependencyProperty]
public static readonly DependencyProperty ItemTemplateSelectorProperty =
DependencyProperty.Register(
"ItemTemplateSelector",
typeof(DataTemplateSelector),
typeof(ItemsControl),
new FrameworkPropertyMetadata(
(DataTemplateSelector) null,
new PropertyChangedCallback(OnItemTemplateSelectorChanged)));
/// <summary>
/// ItemTemplateSelector allows the application writer to provide custom logic
/// for choosing the template used to display each item.
/// </summary>
/// <remarks>
/// This property is ignored if <seealso cref="ItemTemplate"/> is set.
/// </remarks>
[Bindable(true), CustomCategory("Content")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public DataTemplateSelector ItemTemplateSelector
{
get { return (DataTemplateSelector) GetValue(ItemTemplateSelectorProperty); }
set { SetValue(ItemTemplateSelectorProperty, value); }
}
/// <summary>
/// Called when ItemTemplateSelectorProperty is invalidated on "d."
/// </summary>
/// <param name="d">The object on which the property was invalidated.</param>
/// <param name="e">EventArgs that contains the old and new values for this property</param>
private static void OnItemTemplateSelectorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ItemsControl)d).OnItemTemplateSelectorChanged((DataTemplateSelector) e.OldValue, (DataTemplateSelector) e.NewValue);
}
/// <summary>
/// This method is invoked when the ItemTemplateSelector property changes.
/// </summary>
/// <param name="oldItemTemplateSelector">The old value of the ItemTemplateSelector property.</param>
/// <param name="newItemTemplateSelector">The new value of the ItemTemplateSelector property.</param>
protected virtual void OnItemTemplateSelectorChanged(DataTemplateSelector oldItemTemplateSelector, DataTemplateSelector newItemTemplateSelector)
{
CheckTemplateSource();
if ((_itemContainerGenerator != null) && (ItemTemplate == null))
{
_itemContainerGenerator.Refresh();
}
}
/// <summary>
/// The DependencyProperty for the ItemStringFormat property.
/// Flags: None
/// Default Value: null
/// </summary>
[CommonDependencyProperty]
public static readonly DependencyProperty ItemStringFormatProperty =
DependencyProperty.Register(
"ItemStringFormat",
typeof(String),
typeof(ItemsControl),
new FrameworkPropertyMetadata(
(String) null,
new PropertyChangedCallback(OnItemStringFormatChanged)));
/// <summary>
/// ItemStringFormat is the format used to display an item (or a
/// property of an item, as declared by DisplayMemberPath) as a string.
/// This arises only when no template is available.
/// </summary>
[Bindable(true), CustomCategory("Content")]
public String ItemStringFormat
{
get { return (String) GetValue(ItemStringFormatProperty); }
set { SetValue(ItemStringFormatProperty, value); }
}
/// <summary>
/// Called when ItemStringFormatProperty is invalidated on "d."
/// </summary>
private static void OnItemStringFormatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ItemsControl ctrl = (ItemsControl)d;
ctrl.OnItemStringFormatChanged((String) e.OldValue, (String) e.NewValue);
ctrl.UpdateDisplayMemberTemplateSelector();
}
/// <summary>
/// This method is invoked when the ItemStringFormat property changes.
/// </summary>
/// <param name="oldItemStringFormat">The old value of the ItemStringFormat property.</param>
/// <param name="newItemStringFormat">The new value of the ItemStringFormat property.</param>
protected virtual void OnItemStringFormatChanged(String oldItemStringFormat, String newItemStringFormat)
{
}
/// <summary>
/// The DependencyProperty for the ItemBindingGroup property.
/// Flags: None
/// Default Value: null
/// </summary>
[CommonDependencyProperty]
public static readonly DependencyProperty ItemBindingGroupProperty =
DependencyProperty.Register(
"ItemBindingGroup",
typeof(BindingGroup),
typeof(ItemsControl),
new FrameworkPropertyMetadata(
(BindingGroup) null,
new PropertyChangedCallback(OnItemBindingGroupChanged)));
/// <summary>
/// ItemBindingGroup declares a BindingGroup to be used as a "master"
/// for the generated containers. Each container's BindingGroup is set
/// to a copy of the master, sharing the same set of validation rules,
/// but managing its own collection of bindings.
/// </summary>
[Bindable(true), CustomCategory("Content")]
public BindingGroup ItemBindingGroup
{
get { return (BindingGroup) GetValue(ItemBindingGroupProperty); }
set { SetValue(ItemBindingGroupProperty, value); }
}
/// <summary>
/// Called when ItemBindingGroupProperty is invalidated on "d."
/// </summary>
private static void OnItemBindingGroupChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ItemsControl ctrl = (ItemsControl)d;
ctrl.OnItemBindingGroupChanged((BindingGroup) e.OldValue, (BindingGroup) e.NewValue);
}
/// <summary>
/// This method is invoked when the ItemBindingGroup property changes.
/// </summary>
/// <param name="oldItemBindingGroup">The old value of the ItemBindingGroup property.</param>
/// <param name="newItemBindingGroup">The new value of the ItemBindingGroup property.</param>
protected virtual void OnItemBindingGroupChanged(BindingGroup oldItemBindingGroup, BindingGroup newItemBindingGroup)
{
}
/// <summary>
/// Throw if more than one of DisplayMemberPath, xxxTemplate and xxxTemplateSelector
/// properties are set on the given element.
/// </summary>
private void CheckTemplateSource()
{
if (string.IsNullOrEmpty(DisplayMemberPath))
{
Helper.CheckTemplateAndTemplateSelector("Item", ItemTemplateProperty, ItemTemplateSelectorProperty, this);
}
else
{
if (!(this.ItemTemplateSelector is DisplayMemberTemplateSelector))
{
throw new InvalidOperationException(SR.ItemTemplateSelectorBreaksDisplayMemberPath);
}
if (Helper.IsTemplateDefined(ItemTemplateProperty, this))
{
throw new InvalidOperationException(SR.DisplayMemberPathAndItemTemplateDefined);
}
}
}
/// <summary>
/// The DependencyProperty for the ItemContainerStyle property.
/// Flags: none
/// Default Value: null
/// </summary>
[CommonDependencyProperty]
public static readonly DependencyProperty ItemContainerStyleProperty =
DependencyProperty.Register(
"ItemContainerStyle",
typeof(Style),
typeof(ItemsControl),
new FrameworkPropertyMetadata(
(Style) null,
new PropertyChangedCallback(OnItemContainerStyleChanged)));
/// <summary>
/// ItemContainerStyle is the style that is applied to the container element generated
/// for each item.
/// </summary>
[Bindable(true), Category("Content")]
public Style ItemContainerStyle
{
get { return (Style) GetValue(ItemContainerStyleProperty); }
set { SetValue(ItemContainerStyleProperty, value); }
}
/// <summary>
/// Called when ItemContainerStyleProperty is invalidated on "d."
/// </summary>
/// <param name="d">The object on which the property was invalidated.</param>
/// <param name="e">EventArgs that contains the old and new values for this property</param>
private static void OnItemContainerStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ItemsControl) d).OnItemContainerStyleChanged((Style) e.OldValue, (Style) e.NewValue);
}
/// <summary>
/// This method is invoked when the ItemContainerStyle property changes.
/// </summary>
/// <param name="oldItemContainerStyle">The old value of the ItemContainerStyle property.</param>
/// <param name="newItemContainerStyle">The new value of the ItemContainerStyle property.</param>
protected virtual void OnItemContainerStyleChanged(Style oldItemContainerStyle, Style newItemContainerStyle)
{
Helper.CheckStyleAndStyleSelector("ItemContainer", ItemContainerStyleProperty, ItemContainerStyleSelectorProperty, this);
if (_itemContainerGenerator != null)
{
_itemContainerGenerator.Refresh();
}
}
/// <summary>
/// The DependencyProperty for the ItemContainerStyleSelector property.
/// Flags: none
/// Default Value: null
/// </summary>
[CommonDependencyProperty]
public static readonly DependencyProperty ItemContainerStyleSelectorProperty =
DependencyProperty.Register(
"ItemContainerStyleSelector",
typeof(StyleSelector),
typeof(ItemsControl),
new FrameworkPropertyMetadata(
(StyleSelector) null,
new PropertyChangedCallback(OnItemContainerStyleSelectorChanged)));
/// <summary>
/// ItemContainerStyleSelector allows the application writer to provide custom logic
/// to choose the style to apply to each generated container element.
/// </summary>
/// <remarks>
/// This property is ignored if <seealso cref="ItemContainerStyle"/> is set.
/// </remarks>
[Bindable(true), Category("Content")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public StyleSelector ItemContainerStyleSelector
{
get { return (StyleSelector) GetValue(ItemContainerStyleSelectorProperty); }
set { SetValue(ItemContainerStyleSelectorProperty, value); }
}
/// <summary>
/// Called when ItemContainerStyleSelectorProperty is invalidated on "d."
/// </summary>
/// <param name="d">The object on which the property was invalidated.</param>
/// <param name="e">EventArgs that contains the old and new values for this property</param>
private static void OnItemContainerStyleSelectorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ItemsControl) d).OnItemContainerStyleSelectorChanged((StyleSelector) e.OldValue, (StyleSelector) e.NewValue);
}
/// <summary>
/// This method is invoked when the ItemContainerStyleSelector property changes.
/// </summary>
/// <param name="oldItemContainerStyleSelector">The old value of the ItemContainerStyleSelector property.</param>
/// <param name="newItemContainerStyleSelector">The new value of the ItemContainerStyleSelector property.</param>
protected virtual void OnItemContainerStyleSelectorChanged(StyleSelector oldItemContainerStyleSelector, StyleSelector newItemContainerStyleSelector)
{
Helper.CheckStyleAndStyleSelector("ItemContainer", ItemContainerStyleProperty, ItemContainerStyleSelectorProperty, this);
if ((_itemContainerGenerator != null) && (ItemContainerStyle == null))
{
_itemContainerGenerator.Refresh();
}
}
/// <summary>
/// Returns the ItemsControl for which element is an ItemsHost.
/// More precisely, if element is marked by setting IsItemsHost="true"
/// in the style for an ItemsControl, or if element is a panel created
/// by the ItemsPresenter for an ItemsControl, return that ItemsControl.
/// Otherwise, return null.
/// </summary>
public static ItemsControl GetItemsOwner(DependencyObject element)
{
ItemsControl container = null;
Panel panel = element as Panel;
if (panel != null && panel.IsItemsHost)
{
// see if element was generated for an ItemsPresenter
ItemsPresenter ip = ItemsPresenter.FromPanel(panel);
if (ip != null)
{
// if so use the element whose style begat the ItemsPresenter
container = ip.Owner;
}
else
{
// otherwise use element's templated parent
container = panel.TemplatedParent as ItemsControl;
}
}
return container;
}
internal static DependencyObject GetItemsOwnerInternal(DependencyObject element)
{
ItemsControl temp;
return GetItemsOwnerInternal(element, out temp);
}
/// <summary>
/// Different from public GetItemsOwner
/// Returns ip.TemplatedParent instead of ip.Owner
/// More accurate when we want to distinguish if owner is a GroupItem or ItemsControl
/// </summary>
/// <param name="element"></param>
/// <returns></returns>
internal static DependencyObject GetItemsOwnerInternal(DependencyObject element, out ItemsControl itemsControl)
{
DependencyObject container = null;
Panel panel = element as Panel;
itemsControl = null;
if (panel != null && panel.IsItemsHost)
{
// see if element was generated for an ItemsPresenter
ItemsPresenter ip = ItemsPresenter.FromPanel(panel);
if (ip != null)
{
// if so use the element whose style begat the ItemsPresenter
container = ip.TemplatedParent;
itemsControl = ip.Owner;
}
else
{
// otherwise use element's templated parent
container = panel.TemplatedParent;
itemsControl = container as ItemsControl;
}
}
return container;
}
/// <summary>
/// The DependencyProperty for the ItemsPanel property.
/// Flags: none
/// Default Value: null
/// </summary>
[CommonDependencyProperty]
public static readonly DependencyProperty ItemsPanelProperty
= DependencyProperty.Register("ItemsPanel", typeof(ItemsPanelTemplate), typeof(ItemsControl),
new FrameworkPropertyMetadata(GetDefaultItemsPanelTemplate(),
new PropertyChangedCallback(OnItemsPanelChanged)));
private static ItemsPanelTemplate GetDefaultItemsPanelTemplate()
{
ItemsPanelTemplate template = new ItemsPanelTemplate(new FrameworkElementFactory(typeof(StackPanel)));
template.Seal();
return template;
}
/// <summary>
/// ItemsPanel is the panel that controls the layout of items.
/// (More precisely, the panel that controls layout is created
/// from the template given by ItemsPanel.)
/// </summary>
[Bindable(false)]
public ItemsPanelTemplate ItemsPanel
{
get { return (ItemsPanelTemplate) GetValue(ItemsPanelProperty); }
set { SetValue(ItemsPanelProperty, value); }
}
/// <summary>
/// Called when ItemsPanelProperty is invalidated on "d."
/// </summary>
/// <param name="d">The object on which the property was invalidated.</param>
/// <param name="e">EventArgs that contains the old and new values for this property</param>
private static void OnItemsPanelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ItemsControl) d).OnItemsPanelChanged((ItemsPanelTemplate) e.OldValue, (ItemsPanelTemplate) e.NewValue);
}
/// <summary>
/// This method is invoked when the ItemsPanel property changes.
/// </summary>
/// <param name="oldItemsPanel">The old value of the ItemsPanel property.</param>
/// <param name="newItemsPanel">The new value of the ItemsPanel property.</param>
protected virtual void OnItemsPanelChanged(ItemsPanelTemplate oldItemsPanel, ItemsPanelTemplate newItemsPanel)
{
ItemContainerGenerator.OnPanelChanged();
}
private static readonly DependencyPropertyKey IsGroupingPropertyKey =
DependencyProperty.RegisterReadOnly("IsGrouping", typeof(bool), typeof(ItemsControl), new FrameworkPropertyMetadata(BooleanBoxes.FalseBox, new PropertyChangedCallback(OnIsGroupingChanged)));
/// <summary>
/// The DependencyProperty for the IsGrouping property.
/// </summary>
public static readonly DependencyProperty IsGroupingProperty = IsGroupingPropertyKey.DependencyProperty;
/// <summary>
/// Returns whether the control is using grouping.
/// </summary>
[Bindable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool IsGrouping
{
get
{
return (bool)GetValue(IsGroupingProperty);
}
}
private static void OnIsGroupingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ItemsControl)d).OnIsGroupingChanged(e);
}
internal virtual void OnIsGroupingChanged(DependencyPropertyChangedEventArgs e)
{
ShouldCoerceScrollUnitField.SetValue(this, true);
CoerceValue(VirtualizingStackPanel.ScrollUnitProperty);
ShouldCoerceCacheSizeField.SetValue(this, true);
CoerceValue(VirtualizingPanel.CacheLengthUnitProperty);
((IContainItemStorage)this).Clear();
}
/// <summary>
/// The collection of GroupStyle objects that describes the display of
/// each level of grouping. The entry at index 0 describes the top level
/// groups, the entry at index 1 describes the next level, and so forth.
/// If there are more levels of grouping than entries in the collection,
/// the last entry is used for the extra levels.
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ObservableCollection<GroupStyle> GroupStyle
{
get { return _groupStyle; }
}
/// <summary>
/// This method is used by TypeDescriptor to determine if this property should
/// be serialized.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeGroupStyle()
{
return (GroupStyle.Count > 0);
}
private void OnGroupStyleChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (_itemContainerGenerator != null)
{
_itemContainerGenerator.Refresh();
}
}
/// <summary>
/// The DependencyProperty for the GroupStyleSelector property.
/// Flags: none
/// Default Value: null
/// </summary>
public static readonly DependencyProperty GroupStyleSelectorProperty
= DependencyProperty.Register("GroupStyleSelector", typeof(GroupStyleSelector), typeof(ItemsControl),
new FrameworkPropertyMetadata((GroupStyleSelector)null,
new PropertyChangedCallback(OnGroupStyleSelectorChanged)));
/// <summary>
/// GroupStyleSelector allows the app writer to provide custom selection logic
/// for a GroupStyle to apply to each group collection.
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Bindable(true), CustomCategory("Content")]
public GroupStyleSelector GroupStyleSelector
{
get { return (GroupStyleSelector) GetValue(GroupStyleSelectorProperty); }
set { SetValue(GroupStyleSelectorProperty, value); }
}
/// <summary>
/// Called when GroupStyleSelectorProperty is invalidated on "d."
/// </summary>