diff options
author | Bertrand Le Roy <beleroy@microsoft.com> | 2018-06-26 23:02:46 +0300 |
---|---|---|
committer | Bertrand Le Roy <beleroy@microsoft.com> | 2018-06-27 08:34:55 +0300 |
commit | 6e3ca30c02be8fb8d42e8965b6531e8686baf1ef (patch) | |
tree | 769264d43997f579795bdd4fa3b881707212749c /Xamarin.PropertyEditing.Windows | |
parent | bfa7cf328708e5f0599ee1422af99051df4264a1 (diff) |
Trigger change notification from color component boxes only when the user edited the presented value. Fixes #324
Diffstat (limited to 'Xamarin.PropertyEditing.Windows')
-rw-r--r-- | Xamarin.PropertyEditing.Windows/ColorComponentBox.cs | 41 | ||||
-rw-r--r-- | Xamarin.PropertyEditing.Windows/Themes/Resources.xaml | 13 |
2 files changed, 32 insertions, 22 deletions
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}"> - <TextBox.Text> - <MultiBinding Converter="{local:DoubleToQuantityConverter}"> - <Binding Path="Value" RelativeSource="{RelativeSource TemplatedParent}"/> - <Binding Path="Unit" RelativeSource="{RelativeSource TemplatedParent}"/> - </MultiBinding> - </TextBox.Text> </local:TextBoxEx> <local:BrushBoxControl x:Name="brushBoxControl" Visibility="Visible" Brush="{Binding Path=GradientBrush, RelativeSource={RelativeSource TemplatedParent}}" @@ -1169,13 +1163,6 @@ Grid.Row="0" Grid.Column="0" Panel.ZIndex="1"/> </Grid> <ControlTemplate.Triggers> - <DataTrigger Binding="{Binding IsKeyboardFocusWithin, ElementName=innerTextBox}" Value="True"> - <Setter TargetName="innerTextBox" Property="Text" Value="{Binding Path=Value, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, StringFormat={}{0:##0.#}}"/> - <Setter TargetName="brushBoxControl" Property="Visibility" Value="Hidden"/> - </DataTrigger> - <DataTrigger Binding="{Binding IsFocused, ElementName=innerTextBox}" Value="True"> - <Setter TargetName="innerTextBox" Property="Text" Value="{Binding Path=Value, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, StringFormat={}{0:##0.#}}"/> - </DataTrigger> <Trigger Property="GradientBrush" Value="{x:Null}"> <Setter Property="Visibility" TargetName="brushBoxControl" Value="Hidden"/> </Trigger> |