csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.2 KiB
105 lines
3.2 KiB
// -----------------------------------------------------------------------
|
|
// <copyright file="ItemsControl.cs" company="Steven Kirk">
|
|
// Copyright 2014 MIT Licence. See licence.md for more information.
|
|
// </copyright>
|
|
// -----------------------------------------------------------------------
|
|
|
|
namespace Perspex.Controls
|
|
{
|
|
using System;
|
|
using System.Collections;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using Perspex.Controls.Generators;
|
|
using Perspex.Controls.Primitives;
|
|
|
|
public class ItemsControl : TemplatedControl
|
|
{
|
|
private static readonly ItemsPanelTemplate DefaultPanel =
|
|
new ItemsPanelTemplate(() => new StackPanel { Orientation = Orientation.Vertical });
|
|
|
|
public static readonly PerspexProperty<IEnumerable> ItemsProperty =
|
|
PerspexProperty.Register<ItemsControl, IEnumerable>("Items");
|
|
|
|
public static readonly PerspexProperty<ItemsPanelTemplate> ItemsPanelProperty =
|
|
PerspexProperty.Register<ItemsControl, ItemsPanelTemplate>("ItemsPanel", defaultValue: DefaultPanel);
|
|
|
|
private ItemContainerGenerator itemContainerGenerator;
|
|
|
|
public ItemsControl()
|
|
{
|
|
this.GetObservableWithHistory(ItemsProperty).Subscribe(this.ItemsChanged);
|
|
}
|
|
|
|
public ItemContainerGenerator ItemContainerGenerator
|
|
{
|
|
get
|
|
{
|
|
if (this.itemContainerGenerator == null)
|
|
{
|
|
this.itemContainerGenerator = this.CreateItemContainerGenerator();
|
|
}
|
|
|
|
return this.itemContainerGenerator;
|
|
}
|
|
}
|
|
|
|
protected virtual ItemContainerGenerator CreateItemContainerGenerator()
|
|
{
|
|
return new ItemContainerGenerator(this);
|
|
}
|
|
|
|
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); }
|
|
}
|
|
|
|
private void ItemsChanged(Tuple<IEnumerable, IEnumerable> value)
|
|
{
|
|
INotifyPropertyChanged inpc = value.Item1 as INotifyPropertyChanged;
|
|
|
|
if (inpc != null)
|
|
{
|
|
inpc.PropertyChanged -= ItemsPropertyChanged;
|
|
}
|
|
|
|
if (value.Item2 == null || !value.Item2.OfType<object>().Any())
|
|
{
|
|
this.Classes.Add(":empty");
|
|
}
|
|
else
|
|
{
|
|
this.Classes.Remove(":empty");
|
|
}
|
|
|
|
inpc = value.Item2 as INotifyPropertyChanged;
|
|
|
|
if (inpc != null)
|
|
{
|
|
inpc.PropertyChanged += ItemsPropertyChanged;
|
|
}
|
|
}
|
|
|
|
private void ItemsPropertyChanged(object sender, PropertyChangedEventArgs e)
|
|
{
|
|
if (e.PropertyName == "Count")
|
|
{
|
|
if (((IList)sender).Count == 0)
|
|
{
|
|
this.Classes.Add(":empty");
|
|
}
|
|
else
|
|
{
|
|
this.Classes.Remove(":empty");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|