From 6e3ca30c02be8fb8d42e8965b6531e8686baf1ef Mon Sep 17 00:00:00 2001 From: Bertrand Le Roy Date: Tue, 26 Jun 2018 13:02:46 -0700 Subject: Trigger change notification from color component boxes only when the user edited the presented value. Fixes #324 --- .../ColorComponentBox.cs | 41 +++++++++++++++++----- .../Themes/Resources.xaml | 13 ------- 2 files changed, 32 insertions(+), 22 deletions(-) (limited to 'Xamarin.PropertyEditing.Windows') diff --git a/Xamarin.PropertyEditing.Windows/ColorComponentBox.cs b/Xamarin.PropertyEditing.Windows/ColorComponentBox.cs index f095def..6ebf412 100644 --- a/Xamarin.PropertyEditing.Windows/ColorComponentBox.cs +++ b/Xamarin.PropertyEditing.Windows/ColorComponentBox.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using System.Windows; using System.Windows.Controls; using System.Windows.Input; @@ -38,7 +39,12 @@ namespace Xamarin.PropertyEditing.Windows public static readonly DependencyProperty ValueProperty = DependencyProperty.Register ( nameof (Value), typeof (double), typeof (ColorComponentBox), - new PropertyMetadata (0d)); + new PropertyMetadata (0d, OnValueChanged)); + + private static void OnValueChanged (DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is ColorComponentBox colorBox && colorBox.innerTextBox != null) colorBox.SetTextFromValueAndUnit (); + } public double Value { get => (double)GetValue (ValueProperty); @@ -63,22 +69,39 @@ namespace Xamarin.PropertyEditing.Windows if (this.innerTextBox == null) throw new InvalidOperationException ($"{nameof (ColorComponentBox)} is missing a child TextBoxEx named \"innerTextBox\""); + SetTextFromValueAndUnit (); + this.innerTextBox.GotKeyboardFocus += (s, e) => { - this.previousValue = Value; + var textValue = Value.ToString ("##0.#"); + this.previousText = this.innerTextBox.Text = textValue; }; - this.innerTextBox.LostFocus += (s, e) => { - if (Value != this.previousValue) { - RaiseEvent (new RoutedEventArgs (ValueChangedEvent)); - } + this.innerTextBox.LostKeyboardFocus += (s, e) => { + UpdateValueIfChanged (); }; - this.innerTextBox.PreviewKeyDown += (s, e) => { + this.innerTextBox.KeyUp += (s, e) => { if (e.Key == Key.Return) { - RaiseEvent (new RoutedEventArgs (ValueChangedEvent)); + UpdateValueIfChanged (); } }; } private TextBoxEx innerTextBox; - private double previousValue; + private string previousText; + + private void UpdateValueIfChanged() + { + if (this.innerTextBox != null && this.innerTextBox.Text != this.previousText) { + if (double.TryParse (this.innerTextBox.Text, NumberStyles.Float, CultureInfo.CurrentUICulture, out var value)) { + Value = value; + RaiseEvent (new RoutedEventArgs (ValueChangedEvent)); + } + } + SetTextFromValueAndUnit (); + } + + private void SetTextFromValueAndUnit() + { + if (this.innerTextBox != null) this.innerTextBox.Text = Value.ToString ("F0") + Unit; + } } } diff --git a/Xamarin.PropertyEditing.Windows/Themes/Resources.xaml b/Xamarin.PropertyEditing.Windows/Themes/Resources.xaml index cd2374f..2fb4412 100644 --- a/Xamarin.PropertyEditing.Windows/Themes/Resources.xaml +++ b/Xamarin.PropertyEditing.Windows/Themes/Resources.xaml @@ -1156,12 +1156,6 @@ MinWidth="{TemplateBinding MinWidth}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" ToolTip="{TemplateBinding ToolTip}" TabIndex="{TemplateBinding TabIndex}" Tag="{TemplateBinding Tag}" UseLayoutRounding="{TemplateBinding UseLayoutRounding}" Uid="{TemplateBinding Uid}" Grid.Row="0" Grid.Column="0" Panel.ZIndex="0" AutomationProperties.Name="{TemplateBinding AutomationProperties.Name}" AutomationProperties.HelpText="{TemplateBinding AutomationProperties.Name}"> - - - - - - - - - - - - - -- cgit v1.2.3