コード例 #1
0
        /// <summary>
        /// Handles a change in the text. Updates the bindings with Length and Units.
        /// </summary>
        /// <param name="eventArgs">The event arguments</param>
        protected override void OnTextChanged(TextChangedEventArgs eventArgs)
        {
            string[] valueAndUnits = this.Text.Split(new char[] { ' ' }, StringSplitOptions.None);
//			BindingExpression binding;

            base.OnTextChanged(eventArgs);

#if false
            binding = this.GetBindingExpression(TermLength.LengthProperty);
            if (binding != null)
            {
                binding.UpdateTarget();
            }

            binding = this.GetBindingExpression(TermLength.UnitsProperty);
            if (binding != null)
            {
                binding.UpdateTarget();
            }

            if (this.SelectionStart == this.Text.Length && this.Units == null && valueAndUnits.Length > 1 && valueAndUnits[1] != "" && !eventArgs.Changes.Any(c => c.RemovedLength > 0))
            {
                string rest = TermLength.CompleteUnit(valueAndUnits[1]);
                this.SelectionLength = rest.Length;
                this.Text           += rest;
            }
#endif
        }
コード例 #2
0
        /// <summary>
        /// Handle the LostFocus event. If necessary, swap in the last valid value.
        /// </summary>
        /// <param name="eventArgs">The event arguments.</param>
        protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs eventArgs)
        {
            // When the user is done editing, making sure we have valid values for Length and Units.
            if (!this.IsReadOnly)
            {
                string[] valueAndUnits = this.Text.Split(new char[] { ' ' });

                if (valueAndUnits.Length > 1)
                {
                    string unitText = TermLength.CompleteUnit(valueAndUnits[1]);

                    if (!String.IsNullOrEmpty(unitText))
                    {
                        try
                        {
                            this.Units = TimeUnitList.Default.Find((TimeUnit)Enum.Parse(typeof(TimeUnit), unitText, true)).TimeUnitId;
                        }
                        catch
                        {
                            this.Units = null;
                        }
                    }
                }

                if (Regex.Match(valueAndUnits[0], @"^[-+]?\d*\.?\d").Success)
                {
                    Boolean parsed      = false;
                    Decimal parsedValue = 0m;

                    parsed = Decimal.TryParse(valueAndUnits[0], out parsedValue);

                    this.Length = parsed ? (Decimal?)Math.Abs(parsedValue) : null;
                }

                if (this.Length == null)
                {
                    this.Length = this.lastValue;
                }
                if (this.Units == null)
                {
                    this.Units = TimeUnitList.Default.Find(this.lastUnits).TimeUnitId;
                }

                this.Text = String.Format("{0:0.###} {1}", this.Length, this.Units == null ? "" : (Object)(TimeUnitList.Default.Find(this.Units.Value).TimeUnitCode));
            }

            base.OnLostKeyboardFocus(eventArgs);
        }