From 6af31ecf43cffe837b85ffa45661fe81791e2a53 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 28 Jul 2015 21:42:08 +0200 Subject: [PATCH] Use modern Items property changed notification. --- Perspex.Controls/Deck.cs | 4 +- Perspex.Controls/ItemsControl.cs | 68 ++++++++++++++++--- .../Primitives/SelectingItemsControl.cs | 6 +- 3 files changed, 64 insertions(+), 14 deletions(-) diff --git a/Perspex.Controls/Deck.cs b/Perspex.Controls/Deck.cs index 8905d318a3..67d5a83c3d 100644 --- a/Perspex.Controls/Deck.cs +++ b/Perspex.Controls/Deck.cs @@ -35,9 +35,9 @@ namespace Perspex.Controls set { this.SetValue(TransitionProperty, value); } } - protected override void ItemsChanged(IEnumerable oldValue, IEnumerable newValue) + protected override void ItemsChanged(PerspexPropertyChangedEventArgs e) { - base.ItemsChanged(oldValue, newValue); + base.ItemsChanged(e); var items = this.Items; diff --git a/Perspex.Controls/ItemsControl.cs b/Perspex.Controls/ItemsControl.cs index aca64b2ea3..1fbfbacbf3 100644 --- a/Perspex.Controls/ItemsControl.cs +++ b/Perspex.Controls/ItemsControl.cs @@ -19,39 +19,57 @@ namespace Perspex.Controls using Perspex.Controls.Utils; using Perspex.Styling; + /// + /// Displays a collection of items. + /// public class ItemsControl : TemplatedControl, ILogical { + /// + /// The default value for the property. + /// [SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1202:ElementsMustBeOrderedByAccess", Justification = "Needs to be before or a NullReferenceException is thrown.")] private static readonly ItemsPanelTemplate DefaultPanel = new ItemsPanelTemplate(() => new StackPanel()); + /// + /// Defines the property. + /// public static readonly PerspexProperty ItemsProperty = PerspexProperty.Register("Items"); + /// + /// Defines the property. + /// public static readonly PerspexProperty ItemsPanelProperty = PerspexProperty.Register("ItemsPanel", defaultValue: DefaultPanel); private IItemContainerGenerator itemContainerGenerator; - private PerspexReadOnlyListView logicalChildren = + private PerspexReadOnlyListView logicalChildren = new PerspexReadOnlyListView(x => (ILogical)x); private IItemsPresenter presenter; + /// + /// Initializes static members of the class. + /// static ItemsControl() { - ItemsProperty.Changed.Subscribe(e => - { - var control = e.Sender as ItemsControl; - control?.ItemsChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue); - }); + ItemsProperty.Changed.AddClassHandler(x => x.ItemsChanged); } + /// + /// Initializes a new instance of the class. + /// public ItemsControl() { - this.ItemsChanged(null, null); + this.Classes.Add(":empty"); } + /// + /// Gets the for the control. + /// + public IItemContainerGenerator ItemContainerGenerator { get { @@ -64,18 +82,27 @@ namespace Perspex.Controls } } + /// + /// Gets or sets the items to display. + /// public IEnumerable Items { get { return this.GetValue(ItemsProperty); } set { this.SetValue(ItemsProperty, value); } } + /// + /// Gets or sets the panel used to display the items. + /// public ItemsPanelTemplate ItemsPanel { get { return this.GetValue(ItemsPanelProperty); } set { this.SetValue(ItemsPanelProperty, value); } } + /// + /// Gets the items presenter control. + /// public IItemsPresenter Presenter { get @@ -90,6 +117,12 @@ namespace Perspex.Controls } } + /// + /// Gets the logical children of the control. + /// + /// + /// The logical children of an are the item containers. + /// IPerspexReadOnlyList ILogical.LogicalChildren { get @@ -99,25 +132,36 @@ namespace Perspex.Controls } } + /// + /// Creates the for the control. + /// + /// An . protected virtual IItemContainerGenerator CreateItemContainerGenerator() { return new ItemContainerGenerator(this); } + /// protected override void OnTemplateApplied() { this.Presenter = this.FindTemplateChild("itemsPresenter"); } - protected virtual void ItemsChanged(IEnumerable oldValue, IEnumerable newValue) + /// + /// Caled when the property changes. + /// + /// The event args. + protected virtual void ItemsChanged(PerspexPropertyChangedEventArgs e) { - var incc = oldValue as INotifyCollectionChanged; + var incc = e.OldValue as INotifyCollectionChanged; if (incc != null) { incc.CollectionChanged += this.ItemsCollectionChanged; } + var newValue = e.NewValue as IEnumerable; + if (newValue == null || newValue.Count() == 0) { this.Classes.Add(":empty"); @@ -135,6 +179,12 @@ namespace Perspex.Controls } } + /// + /// Called when the event is + /// raised on . + /// + /// The event sender. + /// The event args. protected virtual void ItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { var collection = sender as ICollection; diff --git a/Perspex.Controls/Primitives/SelectingItemsControl.cs b/Perspex.Controls/Primitives/SelectingItemsControl.cs index 8f2b8e1363..2b465e0319 100644 --- a/Perspex.Controls/Primitives/SelectingItemsControl.cs +++ b/Perspex.Controls/Primitives/SelectingItemsControl.cs @@ -86,10 +86,10 @@ namespace Perspex.Controls.Primitives } /// - protected override void ItemsChanged(IEnumerable oldValue, IEnumerable newValue) + protected override void ItemsChanged(PerspexPropertyChangedEventArgs e) { - base.ItemsChanged(oldValue, newValue); - this.SelectedIndex = IndexOf(newValue, this.SelectedItem); + base.ItemsChanged(e); + this.SelectedIndex = IndexOf((IEnumerable)e.NewValue, this.SelectedItem); } ///