// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Controls { using System; using System.Collections; using System.Collections.Generic; using System.Linq; public class ItemsControl : TemplatedControl { private static readonly ItemsPanelTemplate DefaultPanel = new ItemsPanelTemplate(() => new StackPanel { Orientation = Orientation.Vertical }); public static readonly PerspexProperty ItemsProperty = PerspexProperty.Register("Items"); public static readonly PerspexProperty ItemsPanelProperty = PerspexProperty.Register("ItemsPanel", defaultValue: DefaultPanel); private Dictionary itemControls = new Dictionary(); public ItemsControl() { this.GetObservable(ItemsProperty).Subscribe(this.ItemsChanged); } public IEnumerable Items { get { return this.GetValue(ItemsProperty); } set { this.SetValue(ItemsProperty, value); } } public ItemsPanelTemplate ItemsPanel { get { return this.GetValue(ItemsPanelProperty); } set { this.SetValue(ItemsPanelProperty, value); } } public Control GetControlForItem(object item) { Control result; this.itemControls.TryGetValue(item, out result); return result; } public IEnumerable GetAllItemControls() { return this.itemControls.Values; } internal Control CreateItemControl(object item) { Control control = this.CreateItemControlOverride(item); this.itemControls.Add(item, control); return control; } protected virtual Control CreateItemControlOverride(object item) { return (item as Control) ?? this.GetDataTemplate(item).Build(item); } private void ItemsChanged(IEnumerable items) { if (items == null || !items.OfType().Any()) { this.Classes.Add(":empty"); } else { this.Classes.Remove(":empty"); } } } }