Пример #1
0
        public SliderWithTextBoxInput(LocalisableString labelText)
        {
            LabelledTextBox textBox;

            RelativeSizeAxes = Axes.X;
            AutoSizeAxes     = Axes.Y;

            InternalChildren = new Drawable[]
            {
                new FillFlowContainer
                {
                    RelativeSizeAxes = Axes.X,
                    AutoSizeAxes     = Axes.Y,
                    Direction        = FillDirection.Vertical,
                    Spacing          = new Vector2(20),
                    Children         = new Drawable[]
                    {
                        textBox = new LabelledTextBox
                        {
                            Label = labelText,
                        },
                        slider = new SettingsSlider <T>
                        {
                            TransferValueOnCommit = true,
                            RelativeSizeAxes      = Axes.X,
                        }
                    }
                },
            };

            textBox.OnCommit += (t, isNew) =>
            {
                if (!isNew)
                {
                    return;
                }

                try
                {
                    slider.Current.Parse(t.Text);
                }
                catch
                {
                    // TriggerChange below will restore the previous text value on failure.
                }

                // This is run regardless of parsing success as the parsed number may not actually trigger a change
                // due to bindable clamping. Even in such a case we want to update the textbox to a sane visual state.
                Current.TriggerChange();
            };

            Current.BindValueChanged(val =>
            {
                decimal decimalValue = slider.Current.Value.ToDecimal(NumberFormatInfo.InvariantInfo);
                textBox.Text         = decimalValue.ToString($@"N{FormatUtils.FindPrecision(decimalValue)}");
            }, true);
        }