using MultiTerm.Core.ViewModel; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Threading; namespace MultiTerm.Wpf.CustomControl; /// /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file. /// /// Step 1a) Using this custom control in a XAML file that exists in the current project. /// Add this XmlNamespace attribute to the root element of the markup file where it is /// to be used: /// /// xmlns:MyNamespace="clr-namespace:MultiTerm.Wpf.CustomControl.MultiFormatDataView" /// /// /// Step 1b) Using this custom control in a XAML file that exists in a different project. /// Add this XmlNamespace attribute to the root element of the markup file where it is /// to be used: /// /// xmlns:MyNamespace="clr-namespace:MultiTerm.Wpf.CustomControl.MultiFormatDataView;assembly=MultiTerm.Wpf.CustomControl.MultiFormatDataView" /// /// You will also need to add a project reference from the project where the XAML file lives /// to this project and Rebuild to avoid compilation errors: /// /// Right click on the target project in the Solution Explorer and /// "Add Reference"->"Projects"->[Browse to and select this project] /// /// /// Step 2) /// Go ahead and use your control in the XAML file. /// /// /// /// public class MultiFormatDataView : Control { private static readonly Dictionary itemParentPairs = new(); private const string itemsControlTemplateName = "itemsControl"; private ItemsControl? itemsControl; #region Dependency Properties public static readonly DependencyProperty DataSourceProperty = DependencyProperty.Register("DataSource", typeof(IEnumerable), typeof(MultiFormatDataView), new PropertyMetadata(null, OnDataSourcePropertyChanged)); public static readonly DependencyProperty RealizedItemsCountProperty = DependencyProperty.Register("RealizedItemsCount", typeof(uint), typeof(MultiFormatDataView), new PropertyMetadata((uint)0, OnRealizedItemsCountChanged)); public static readonly DependencyProperty ItemLoadedProperty = DependencyProperty.RegisterAttached("ItemLoaded", typeof(bool), typeof(MultiFormatDataView), new UIPropertyMetadata(false, OnItemLoaded)); public static readonly DependencyProperty ItemUnloadedProperty = DependencyProperty.RegisterAttached("ItemUnloaded", typeof(bool), typeof(MultiFormatDataView), new UIPropertyMetadata(false, OnItemUnloaded)); /// /// .NET Property for DataSource. /// [Bindable(true)] public IEnumerable DataSource { get { return (IEnumerable)GetValue(DataSourceProperty); } set { SetValue(DataSourceProperty, value); } } /// /// .NET Property for RealizedItemsCount. /// [Bindable(true)] public uint RealizedItemsCount { get { return (uint)GetValue(RealizedItemsCountProperty); } set { SetValue(RealizedItemsCountProperty, value); } } #endregion static MultiFormatDataView() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiFormatDataView), new FrameworkPropertyMetadata(typeof(MultiFormatDataView))); } public override void OnApplyTemplate() { base.OnApplyTemplate(); // get itemsControl from template if (GetTemplateChild(itemsControlTemplateName) is ItemsControl itemsControl) { this.itemsControl = itemsControl; } else { throw new Exception($"Implementation fault, {itemsControlTemplateName} not found in template."); } } private static void OnDataSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // extract instance and guard null if (d is not MultiFormatDataView mfdv) { return; } // extract instance of new Value and guard null if (e.NewValue is not IEnumerable enumerable) { return; } // check if enumerable items are all of correct type foreach (var item in enumerable) { if (item is not DataViewModel) { throw new ArgumentException($"{nameof(DataSourceProperty)} must be of type {nameof(DataViewModel)}"); } } // add group property CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(e.NewValue); PropertyGroupDescription groupDescription = new(nameof(DataViewModel.LineIdentifier)); view.GroupDescriptions.Add(groupDescription); } private static void OnRealizedItemsCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // NOP } private static void OnItemLoaded(DependencyObject d, DependencyPropertyChangedEventArgs e) { // extract instance and guard null if (d is not StackPanel stackPanel) { return; } // check if value was set to true if (e.NewValue is bool boolean && boolean == true) { // find visual parent of correct type, throw exception if not found var parentMFDV = UIHelper.FindVisualParent(stackPanel) ?? throw new NullReferenceException($"Could not find parent of type " + $"{nameof(MultiFormatDataView)} in {nameof(stackPanel)}"); // add to static dictionary itemParentPairs.Add(stackPanel, parentMFDV); // increment counter parentMFDV.RealizedItemsCount++; } } private static void OnItemUnloaded(DependencyObject d, DependencyPropertyChangedEventArgs e) { // extract instance and guard null if (d is not StackPanel stackPanel) { return; } // check if value was set to true if (e.NewValue is bool boolean && boolean == true) { // get parent from static dictionary var parentMFDV = itemParentPairs[stackPanel]; // remove the element from the dictionary itemParentPairs.Remove(stackPanel); // decrement counter parentMFDV.RealizedItemsCount--; } } }