SimpleItemsControl.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System.Collections;
  2. using System.Collections.ObjectModel;
  3. using System.Collections.Specialized;
  4. using System.ComponentModel;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Markup;
  8. using HandyControl.Data;
  9. namespace HandyControl.Controls;
  10. /// <summary>
  11. /// ItemsControl的轻量级版本
  12. /// </summary>
  13. [DefaultProperty("Items")]
  14. [ContentProperty("Items")]
  15. [TemplatePart(Name = ElementPanel, Type = typeof(Panel))]
  16. public class SimpleItemsControl : Control
  17. {
  18. private const string ElementPanel = "PART_Panel";
  19. public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register(
  20. nameof(ItemTemplate), typeof(DataTemplate), typeof(SimpleItemsControl),
  21. new FrameworkPropertyMetadata(default(DataTemplate), OnItemTemplateChanged));
  22. public static readonly DependencyProperty ItemContainerStyleProperty = DependencyProperty.Register(
  23. nameof(ItemContainerStyle), typeof(Style), typeof(SimpleItemsControl),
  24. new PropertyMetadata(default(Style), OnItemContainerStyleChanged));
  25. public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
  26. nameof(ItemsSource), typeof(IEnumerable), typeof(SimpleItemsControl),
  27. new PropertyMetadata(default(IEnumerable), OnItemsSourceChanged));
  28. public SimpleItemsControl()
  29. {
  30. var items = new ObservableCollection<object>();
  31. items.CollectionChanged += (s, e) =>
  32. {
  33. if (e.NewItems != null && e.NewItems.Count > 0)
  34. {
  35. SetValue(HasItemsPropertyKey, ValueBoxes.TrueBox);
  36. }
  37. OnItemsChanged(s, e);
  38. };
  39. Items = items;
  40. }
  41. public IEnumerable ItemsSource
  42. {
  43. get => (IEnumerable) GetValue(ItemsSourceProperty);
  44. set => SetValue(ItemsSourceProperty, value);
  45. }
  46. [Bindable(true)]
  47. [Category("Content")]
  48. public Style ItemContainerStyle
  49. {
  50. get => (Style) GetValue(ItemContainerStyleProperty);
  51. set => SetValue(ItemContainerStyleProperty, value);
  52. }
  53. [Bindable(true)]
  54. public DataTemplate ItemTemplate
  55. {
  56. get => (DataTemplate) GetValue(ItemTemplateProperty);
  57. set => SetValue(ItemTemplateProperty, value);
  58. }
  59. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  60. [Bindable(true)]
  61. public Collection<object> Items { get; }
  62. internal Panel ItemsHost { get; set; }
  63. private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  64. {
  65. ((SimpleItemsControl) d).OnItemsSourceChanged((IEnumerable) e.OldValue, (IEnumerable) e.NewValue);
  66. }
  67. protected virtual void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
  68. {
  69. }
  70. public override void OnApplyTemplate()
  71. {
  72. ItemsHost?.Children.Clear();
  73. base.OnApplyTemplate();
  74. ItemsHost = GetTemplateChild(ElementPanel) as Panel;
  75. Refresh();
  76. }
  77. protected virtual void OnItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
  78. {
  79. Refresh();
  80. UpdateItems();
  81. }
  82. protected virtual DependencyObject GetContainerForItemOverride() => new ContentPresenter();
  83. protected virtual bool IsItemItsOwnContainerOverride(object item) => item is ContentPresenter;
  84. protected virtual void PrepareContainerForItemOverride(DependencyObject element, object item)
  85. {
  86. switch (element)
  87. {
  88. case ContentControl contentControl:
  89. contentControl.Content = item;
  90. contentControl.ContentTemplate = ItemTemplate;
  91. break;
  92. case ContentPresenter contentPresenter:
  93. contentPresenter.Content = item;
  94. contentPresenter.ContentTemplate = ItemTemplate;
  95. break;
  96. }
  97. }
  98. private static void OnItemTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  99. => (d as SimpleItemsControl)?.OnItemTemplateChanged(e);
  100. protected virtual void OnItemTemplateChanged(DependencyPropertyChangedEventArgs e) => Refresh();
  101. private static void OnItemContainerStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  102. => (d as SimpleItemsControl)?.OnItemContainerStyleChanged(e);
  103. protected virtual void OnItemContainerStyleChanged(DependencyPropertyChangedEventArgs e) => Refresh();
  104. internal static readonly DependencyPropertyKey HasItemsPropertyKey =
  105. DependencyProperty.RegisterReadOnly(nameof(HasItems), typeof(bool), typeof(SimpleItemsControl),
  106. new PropertyMetadata(ValueBoxes.FalseBox));
  107. public static readonly DependencyProperty HasItemsProperty = HasItemsPropertyKey.DependencyProperty;
  108. public bool HasItems => (bool) GetValue(HasItemsProperty);
  109. protected virtual void Refresh()
  110. {
  111. if (ItemsHost == null) return;
  112. ItemsHost.Children.Clear();
  113. foreach (var item in Items)
  114. {
  115. DependencyObject container;
  116. if (IsItemItsOwnContainerOverride(item))
  117. {
  118. container = item as DependencyObject;
  119. }
  120. else
  121. {
  122. container = GetContainerForItemOverride();
  123. PrepareContainerForItemOverride(container, item);
  124. }
  125. if (container is FrameworkElement element)
  126. {
  127. element.Style = ItemContainerStyle;
  128. ItemsHost.Children.Add(element);
  129. }
  130. }
  131. }
  132. protected virtual void UpdateItems()
  133. {
  134. }
  135. }