From 8bd6a142974b9b899171518ea4cc6cb318e656a4 Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Fri, 14 Apr 2023 10:58:11 +0200 Subject: [PATCH] worked on MultiFormatTextBox --- .../Serial/SerialProtocolSettingsViewModel.cs | 5 ++ .../MultiFormatTextBox/MultiFormatTextBox.cs | 90 ++++++++++++++++++- .../MultiFormatTextBox.xaml | 35 +++----- .../ValueConverters/IntToStringConverter.cs | 10 ++- 4 files changed, 114 insertions(+), 26 deletions(-) diff --git a/MultiTerm.Protocols/Serial/SerialProtocolSettingsViewModel.cs b/MultiTerm.Protocols/Serial/SerialProtocolSettingsViewModel.cs index 34af6c9..b7d8264 100644 --- a/MultiTerm.Protocols/Serial/SerialProtocolSettingsViewModel.cs +++ b/MultiTerm.Protocols/Serial/SerialProtocolSettingsViewModel.cs @@ -67,6 +67,11 @@ public partial class SerialProtocolSettingsViewModel : ProtocolSettingsViewModel this.messenger.Send(new ProtocolSettingsInvalidMessage(nameof(this.DataBits), "Must be 5...8 or 16")); return false; } + if(this.BaudRate < 0) + { + this.messenger.Send(new ProtocolSettingsInvalidMessage(nameof(this.BaudRate), "Must be larger than 0")); + return false; + } return true; } diff --git a/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs b/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs index bf0cf9d..cc274f8 100644 --- a/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs +++ b/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs @@ -1,5 +1,11 @@ -using System.Windows; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Windows; using System.Windows.Controls; +using System.Windows.Input; +using System.Windows.Media; namespace MultiTerm.Wpf.CustomControl; @@ -32,10 +38,90 @@ namespace MultiTerm.Wpf.CustomControl; /// /// /// -public class MultiFormatTextBox : TextBox +internal class Format { + public string Name { get; set; } + public Brush BackgroundBrush { get; set; } + public Predicate IsKeyValid { get; set; } + public Format(string name, Brush backgroundBrush, Predicate keyValidator) + { + this.Name = name; + this.BackgroundBrush = backgroundBrush; + this.IsKeyValid = keyValidator; + } + public static List GetListOfNames(IEnumerable formats) + { + return formats.Select(item => item.Name).ToList(); + } +} + +public class MultiFormatTextBox : Control +{ + private static readonly List formats = new() + { + // character input, accepts all keys + new Format("CHAR", Brushes.White, delegate(Key k) { return true; }), + // hex input, ignores all keys that are not inbetween 0 and F + new Format("HEX", Brushes.Orange, delegate(Key k) { return (k >= Key.D0 && k <= Key.F); }), + // binary input, ignores all keys except 0 and 1 + new Format("BIN", Brushes.AliceBlue, delegate(Key k) { return (k == Key.D0 || k == Key.D1); }) + }; + + private ComboBox comboBox; + private RichTextBox richTextBox; + private Format currentlySelectedFormat = formats.First(); + static MultiFormatTextBox() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MultiFormatTextBox), new FrameworkPropertyMetadata(typeof(MultiFormatTextBox))); } + + public override void OnApplyTemplate() + { + base.OnApplyTemplate(); + + // get comboBox from template + var comboBox = GetTemplateChild("comboBox") as ComboBox; + if (comboBox != null) + { + this.comboBox = comboBox; + this.comboBox.ItemsSource = Format.GetListOfNames(formats); + this.comboBox.SelectedItem = currentlySelectedFormat.Name; + this.comboBox.SelectionChanged += ComboBox_SelectionChanged; + } + + // get richTextBox from template + var richTextBox = GetTemplateChild("richTextBox") as RichTextBox; + if (richTextBox != null) + { + this.richTextBox = richTextBox; + this.richTextBox.KeyDown += RichTextBox_KeyDown; + } + + } + + private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + // match all formats with the name selected in the combobox + var matchingFormats = formats.Where(format => format.Name == (string)this.comboBox!.SelectedItem); + // check if exactly one format was matched + if(matchingFormats.Count() != 1) + { + throw new Exception($"{nameof(ComboBox_SelectionChanged)} could not match a correct amount of formats"); + } + // set currently selected format + this.currentlySelectedFormat = matchingFormats.First(); + } + + private void RichTextBox_KeyDown(object sender, KeyEventArgs e) + { + // guard combobox null + if (comboBox == null) throw new Exception($"{nameof(comboBox)} cannot be null"); + + // if key is invalid for this format => ignore it (handled = true) + if(this.currentlySelectedFormat.IsKeyValid(e.Key) == false) + { + e.Handled = true; + } + } } diff --git a/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.xaml b/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.xaml index 77c27b4..5e85855 100644 --- a/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.xaml +++ b/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.xaml @@ -2,11 +2,11 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MultiTerm.Wpf.CustomControl"> - + \ No newline at end of file diff --git a/MultiTerm.Wpf/ValueConverters/IntToStringConverter.cs b/MultiTerm.Wpf/ValueConverters/IntToStringConverter.cs index 2829f5e..76bf61c 100644 --- a/MultiTerm.Wpf/ValueConverters/IntToStringConverter.cs +++ b/MultiTerm.Wpf/ValueConverters/IntToStringConverter.cs @@ -17,6 +17,14 @@ public class IntToStringConverter : IValueConverter { // guard type if (value is not string stringVal) { throw new ArgumentException("Can only convert from string value"); } - return int.Parse(stringVal); + + // try parsing to int + int parsedVal; + if(int.TryParse(stringVal, out parsedVal) == false) + { + // error + parsedVal = -1; + } + return parsedVal; } }