|
|
|
|
@ -1,10 +1,12 @@ |
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
@ -37,8 +39,10 @@ namespace MultiTerm.Wpf.CustomControl; |
|
|
|
|
/// <MyNamespace:MultiFormatDataView/> |
|
|
|
|
/// |
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
|
|
public class MultiFormatDataView : Control |
|
|
|
|
{ |
|
|
|
|
private static readonly Dictionary<StackPanel, MultiFormatDataView> itemParentPairs = new(); |
|
|
|
|
private const string itemsControlTemplateName = "itemsControl"; |
|
|
|
|
private ItemsControl? itemsControl; |
|
|
|
|
|
|
|
|
|
@ -48,6 +52,23 @@ public class MultiFormatDataView : Control |
|
|
|
|
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)); |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// .NET Property for DataSource. |
|
|
|
|
/// </summary> |
|
|
|
|
@ -58,6 +79,16 @@ public class MultiFormatDataView : Control |
|
|
|
|
set { SetValue(DataSourceProperty, value); } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// .NET Property for RealizedItemsCount. |
|
|
|
|
/// </summary> |
|
|
|
|
[Bindable(true)] |
|
|
|
|
public uint RealizedItemsCount |
|
|
|
|
{ |
|
|
|
|
get { return (uint)GetValue(RealizedItemsCountProperty); } |
|
|
|
|
set { SetValue(RealizedItemsCountProperty, value); } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
static MultiFormatDataView() |
|
|
|
|
@ -65,6 +96,7 @@ public class MultiFormatDataView : Control |
|
|
|
|
DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiFormatDataView), new FrameworkPropertyMetadata(typeof(MultiFormatDataView))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void OnApplyTemplate() |
|
|
|
|
{ |
|
|
|
|
base.OnApplyTemplate(); |
|
|
|
|
@ -78,7 +110,6 @@ public class MultiFormatDataView : Control |
|
|
|
|
{ |
|
|
|
|
throw new Exception($"Implementation fault, {itemsControlTemplateName} not found in template."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void OnDataSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
|
|
|
|
@ -100,4 +131,47 @@ public class MultiFormatDataView : Control |
|
|
|
|
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<MultiFormatDataView>(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--; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|