diff --git a/MultiTerm.Core/ViewModel/CommunicationDataViewModel.cs b/MultiTerm.Core/ViewModel/CommunicationDataViewModel.cs index d3e3ea6..db28987 100644 --- a/MultiTerm.Core/ViewModel/CommunicationDataViewModel.cs +++ b/MultiTerm.Core/ViewModel/CommunicationDataViewModel.cs @@ -93,13 +93,13 @@ public partial class CommunicationDataViewModel : ObservableObject } [RelayCommand] - private void ClearReceivedCharacters() + private void ClearReceivedData() { this.ReceivedData = new ObservableCollection(); } [RelayCommand] - private void ClearSentCharacters() + private void ClearSentData() { this.SentData = new ObservableCollection(); } diff --git a/MultiTerm.Wpf.CustomControl/ExtendedTabControl/ExtendedTabControl.cs b/MultiTerm.Wpf.CustomControl/ExtendedTabControl/ExtendedTabControl.cs index 7eba2fb..f3d75d7 100644 --- a/MultiTerm.Wpf.CustomControl/ExtendedTabControl/ExtendedTabControl.cs +++ b/MultiTerm.Wpf.CustomControl/ExtendedTabControl/ExtendedTabControl.cs @@ -3,35 +3,6 @@ using System.Windows.Controls; 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.Controls" -/// -/// -/// 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.Controls;assembly=MultiTerm.Wpf.Controls" -/// -/// 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 ExtendedTabControl : TabControl { public static readonly RoutedEvent AddButtonClickedEvent; diff --git a/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs b/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs index 324321f..67bf6ce 100644 --- a/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs +++ b/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs @@ -2,50 +2,18 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.ComponentModel; -using System.Diagnostics; -using System.Runtime.CompilerServices; 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 static readonly Dictionary itemParentPairs = new(); private const string itemsControlTemplateName = "itemsControl"; + private const string buttonClearTemplateName = "btnClear"; private ListBox? itemsControl; private List currentSelectedItems = new(); @@ -80,6 +48,8 @@ public class MultiFormatDataView : Control typeof(MultiFormatDataView), new UIPropertyMetadata(false, OnItemUnloaded)); + public static readonly RoutedEvent ClearRequestedEvent; + /// /// .NET Property for DataSource. /// @@ -127,11 +97,24 @@ public class MultiFormatDataView : Control get { return (bool)GetValue(ItemUnloadedProperty); } set { SetValue(ItemUnloadedProperty, value); } } + + /// + /// .NET Property for + /// + public event RoutedEventHandler ClearRequested + { + add { this.AddHandler(ClearRequestedEvent, value); } + remove { this.RemoveHandler(ClearRequestedEvent, value); } + } #endregion static MultiFormatDataView() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiFormatDataView), new FrameworkPropertyMetadata(typeof(MultiFormatDataView))); + + ClearRequestedEvent = EventManager.RegisterRoutedEvent("ClearRequested", + RoutingStrategy.Bubble, typeof(RoutedEventArgs), + typeof(MultiFormatDataView)); } public override void OnApplyTemplate() @@ -149,8 +132,41 @@ public class MultiFormatDataView : Control { throw new Exception($"Implementation fault, {itemsControlTemplateName} not found in template."); } + + // get button from template, ignore if it does not exist + if (GetTemplateChild(buttonClearTemplateName) is Button button) + { + button.Click += OnClearButtonClicked; ; + } } + 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 void OnClearButtonClicked(object sender, RoutedEventArgs e) + { + // raise clear requested event + RoutedEventArgs args = new(ClearRequestedEvent); + RaiseEvent(args); + } + + #region Selected Items handling private void ItemsControl_SelectionChanged(object sender, SelectionChangedEventArgs e) { if(this.currentSelectedItems == null) @@ -174,29 +190,9 @@ public class MultiFormatDataView : Control { // NOP } + #endregion - - 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); - } - - + #region Realized Item Count private static void OnRealizedItemsCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // NOP @@ -238,4 +234,5 @@ public class MultiFormatDataView : Control parentMFDV.RealizedItemsCount--; } } + #endregion } diff --git a/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.xaml b/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.xaml index 129f4eb..e019d7f 100644 --- a/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.xaml +++ b/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.xaml @@ -119,6 +119,7 @@ +