Example #1
0
        /// <summary>
        /// create a TextBox limited to numbers with Up/Down controls to adjust value and binds to given property
        /// </summary>
        /// <param name="boundPropName">property name value is bound to</param>
        /// <param name="tooltip">long description of what this item represents</param>
        /// <param name="numberType"></param>
        /// <returns>initialized NumberBox control</returns>
        private NumberBox GetNumberBox(string boundPropName, Object toolTip, NumberType numberType)
        {
            try
            {
                var nb = new NumberBox();
                if (toolTip != null)
                {
                    nb.ToolTip = toolTip;
                }

                // bind value to given property
                var binding = new Binding(boundPropName)
                {
                    Mode = BindingMode.TwoWay,
                    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                };
                nb.SetBinding(NumberBox.NumberProperty, binding);

                nb.NumberType = numberType;

                nb.Minimum = 0;
                //nb.Maximum = ?;
                nb.DefaultNumber = 0;

                // can bind to disable until some condition is met
                nb.HasCheckBox      = false;
                nb.CheckBoxBehavior = NumberBoxCheckBoxBehavior.None;
                nb.IsChecked        = false; // bind me
                nb.CheckBoxPosition = Dock.Left;

                // some other properties that normally don't need to be changed
                nb.Step = 1;
                nb.PredefinesCulture       = System.Globalization.CultureInfo.GetCultureInfo("en-US");
                nb.NumberSelectionBehavior = NumberBoxSelection.OnFocusAndUpDown;
                nb.UpDownButtonsPosition   = Dock.Right;
                nb.UpDownBehavior          = UpDownBehavior.ArrowsAndButtons;

                nb.LostFocusBehavior = new LostFocusBehavior(ValueBehavior.PlaceDefaultNumber)
                {
                    TrimLeadingZero = true
                                      //FormatText={}{0:D2}}
                };

                return(nb);
            }
            catch (Exception e)
            {
                logger.Warn(e);
                throw;
            }
        }