From c11c99b3c8a81af19a5ac0c60e66bdd9a00c9d3d Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Thu, 20 Apr 2023 08:16:01 +0200 Subject: [PATCH] added MultiFormatDataView in first state, fixed some warnings --- .../MultiFormatDataView.cs | 83 +++++++++++ .../MultiFormatDataView.xaml | 133 ++++++++++++++++++ .../MultiFormatTextBox/MultiFormatTextBox.cs | 10 +- .../MultiTerm.Wpf.CustomControl.csproj | 4 + .../Themes/Generic.xaml | 1 + 5 files changed, 225 insertions(+), 6 deletions(-) create mode 100644 MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs create mode 100644 MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.xaml diff --git a/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs b/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs new file mode 100644 index 0000000..9af48cd --- /dev/null +++ b/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs @@ -0,0 +1,83 @@ +using MultiTerm.Protocols.Model; +using System; +using System.Collections; +using System.ComponentModel; +using System.Runtime.CompilerServices; +using System.Windows; +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.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 +{ + #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(); + } + 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 DataLine) + { throw new ArgumentException($"{nameof(DataSourceProperty)} must be of type {nameof(DataLine)}"); } + } + } +} diff --git a/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.xaml b/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.xaml new file mode 100644 index 0000000..4f01afb --- /dev/null +++ b/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.xaml @@ -0,0 +1,133 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs b/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs index bba58c1..091daf8 100644 --- a/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs +++ b/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs @@ -71,8 +71,7 @@ public class MultiFormatTextBox : Control private ComboBox? comboBox; private RichTextBox? richTextBox; private Format? currentlySelectedFormat; - private int offsetContentStartToFormatStart; - private object lockObj = new(); + private readonly object lockObj = new(); #endregion @@ -128,7 +127,6 @@ public class MultiFormatTextBox : Control this.richTextBox.AcceptsReturn = false; this.richTextBox.KeyDown += RichTextBox_KeyDown; this.richTextBox.TextChanged += RichTextBox_TextChanged; - this.offsetContentStartToFormatStart = 0; } else { @@ -139,7 +137,7 @@ public class MultiFormatTextBox : Control private static void OnCurrentMultiFormatStringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // extract instance and guard null - if (d is not MultiFormatTextBox mftb) { return; } + // if (d is not MultiFormatTextBox mftb) { return; } // nothing to do } @@ -244,7 +242,7 @@ public class MultiFormatTextBox : Control } // update CurrentMultiFormatString - this.CurrentMultiFormatString = this.ConvertRtbContentToMultiFormatString(this.richTextBox!); + this.CurrentMultiFormatString = ConvertRtbContentToMultiFormatString(this.richTextBox!); } } @@ -264,7 +262,7 @@ public class MultiFormatTextBox : Control rtb.Selection.Select(end, end); } - private MultiFormatString ConvertRtbContentToMultiFormatString(RichTextBox rtb) + private static MultiFormatString ConvertRtbContentToMultiFormatString(RichTextBox rtb) { MultiFormatString multiFormatString = new(); // store current caret position diff --git a/MultiTerm.Wpf.CustomControl/MultiTerm.Wpf.CustomControl.csproj b/MultiTerm.Wpf.CustomControl/MultiTerm.Wpf.CustomControl.csproj index f432ab3..e29b1fc 100644 --- a/MultiTerm.Wpf.CustomControl/MultiTerm.Wpf.CustomControl.csproj +++ b/MultiTerm.Wpf.CustomControl/MultiTerm.Wpf.CustomControl.csproj @@ -6,6 +6,10 @@ true + + + + diff --git a/MultiTerm.Wpf.CustomControl/Themes/Generic.xaml b/MultiTerm.Wpf.CustomControl/Themes/Generic.xaml index eccd603..1f42123 100644 --- a/MultiTerm.Wpf.CustomControl/Themes/Generic.xaml +++ b/MultiTerm.Wpf.CustomControl/Themes/Generic.xaml @@ -7,6 +7,7 @@ +