using MultiTerm.Core.ViewModel; using System; using System.Collections; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Data; 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 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)); /// /// .NET Property for DataSource. /// [Bindable(true)] public IEnumerable DataSource { get { return (IEnumerable)GetValue(DataSourceProperty); } set { SetValue(DataSourceProperty, 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); } }