diff --git a/src/client/spark/view/Controls/PriceEntryControl.xaml.cs b/src/client/spark/view/Controls/PriceEntryControl.xaml.cs
index b02202ec9..5f13adde3 100644
--- a/src/client/spark/view/Controls/PriceEntryControl.xaml.cs
+++ b/src/client/spark/view/Controls/PriceEntryControl.xaml.cs
@@ -1,24 +1,44 @@
using bts.api;
+using bts.utils;
using mbts.spark;
-using mbts.ui.extensions;
using mbts.ui.spark.converters;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
-using static mbts.ui.utility.text.TextUtils;
namespace mbts.ui.spark {
///
/// Interaction logic for PriceEntryControl.xaml
///
class PriceEntryControl : Control {
+ double? _raw_value;
+
static PriceEntryControl() =>
DefaultStyleKeyProperty.OverrideMetadata(typeof(PriceEntryControl), new FrameworkPropertyMetadata(typeof(PriceEntryControl)));
#region Value
- public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(PriceEntryControl),new FrameworkPropertyMetadata(double.NaN, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, CoerceValue));
- private static object CoerceValue(DependencyObject d, object baseValue) { return TickUtils.get_regional_ticks((ContractSpecificationImmutable)d.GetValue(ContractSpecProperty)).round((double)baseValue); }
+ public static readonly DependencyProperty ValueProperty =
+ WPFHelper.create_dp(
+ nameof(Value),
+ FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
+ null,
+ (o, v) => {
+ var spec = o.ContractSpec;
+ o._raw_value = v;
+ if (!spec.HasValue || DoubleHelper.IsNaNOrInfinity(v))
+ return v;
+ // Note: the decimal cast here is important bc double-rounding errors can cause the keyboard text to conflict with poorly rounded result, and negate Tick Up/Down commands.
+ // This happens bc the tick up/down commands flush the current text before inc/decrementing.
+ double tick = (double)spec.Value.round_to_tick((decimal)v);
+ if (MathUtils.epsilon_equals(tick, v)) {
+ // this value has already been rounded, don't make it re-enter the CoerceValueCallback delegate.
+ return v;
+ }
+ return tick;
+ },
+ double.NaN);
+
public double Value { get => (double)GetValue(ValueProperty); set => SetValue(ValueProperty, value); }
#endregion
@@ -40,9 +60,20 @@ namespace mbts.ui.spark {
#endregion
#region ContractSpec
- public static readonly DependencyProperty ContractSpecProperty = DependencyProperty.Register("ContractSpec", typeof(ContractSpecificationImmutable), typeof(PriceEntryControl),
- new PropertyMetadata(SecurityDataHelper.Defaults.ContractSpecificationImmutablePennies, OnContractSpecChanged));
- static void OnContractSpecChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => ((PriceEntryControl)d)._update_text_binding();
+ public static readonly DependencyProperty ContractSpecProperty =
+ WPFHelper.create_dp(
+ nameof(ContractSpec),
+ (o, v) => {
+ if (o._raw_value.HasValue) {
+ // Refresh Value (rounding may have changed)
+ // Note: We don't call 'CoerceValue' bc the ValueProperty's CoerceValueCallback always overwrites '_raw_value.'
+ // If we called CoerceValue here, the callback would ultimately overwrite _raw_value with the previous ContractSpec's rounded value.
+ // This is a chicken/egg dilemma; we want to ensure Value is always round with respect to the ContractSpecification.
+ o.SetCurrentValue(ValueProperty, o._raw_value.Value);
+ }
+ o._update_text_binding();
+ }, SecurityDataHelper.Defaults.ContractSpecificationImmutable);
+
public ContractSpecificationImmutable? ContractSpec { get => GetValue(ContractSpecProperty) as ContractSpecificationImmutable?; set => SetValue(ContractSpecProperty, value); }
#endregion
diff --git a/src/client/spark/view/Spark/Core/TickUtils.cs b/src/client/spark/view/Spark/Core/TickUtils.cs
index 180f1a3b1..4c2d0457c 100644
--- a/src/client/spark/view/Spark/Core/TickUtils.cs
+++ b/src/client/spark/view/Spark/Core/TickUtils.cs
@@ -123,6 +123,14 @@ namespace mbts.spark {
return intpart + floatpart;
}
+ public static decimal round_to_tick(this ContractSpecificationImmutable spec, decimal value) {
+ decimal intpart = (long)value;
+ decimal floatpart = value % 1;
+ decimal tick = (decimal)spec.ticksize((double)value);
+ floatpart = ((int)(floatpart / tick)) * tick;
+ return intpart + floatpart;
+ }
+
public static RegionalTickFactors get_regional_ticks(ContractSpecificationImmutable spec) =>
TickStore.get(spec);