0

我正在开发 WPF 应用程序,其中有一个文本框、按钮、标签和一个切换按钮。在我的应用程序中,我应该执行如下两个操作:

  1. 仅将文本框的输入设置在 0.0 到 5.0 之间。因此,如果写入的任何值超过 5.0,则必须限制为 5。类似地,写入低于 0.0 的任何值都必须受到限制,并且只能显示 0.0。

  2. 如果值为 3.4 V,我的标签应将内容显示为“值”VIe,因为伏特应与其连接。

Xaml 在这里:

<TextBox Grid.Column="1" Text="{Binding VoltageText}" Name="VoltageBox" />        
<Label Grid.Column="2" Content="{Binding CurrentText}" Name="CurrentLabel" />

型号类:

string voltageText = string.Empty;
    public string VoltageText
    {
        get
        {
            return voltageText;
        }

        set
        {
            voltageText = value;
            OnPropertyChanged("VoltageText");
        }
    }

这个文本框必须有我在第一点中提到的输入限制:) 我写的 textchanged 方法只允许 0-9 和 . 在文本框中:

// present in xaml.cs
private void VoltageBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        string txt = VoltageBox.Text;
        if (txt != "")
        {
            VoltageBox.Text = Regex.Replace(VoltageBox.Text, "[^0-9] [^.]", "");
            if (txt != VoltageBox.Text)
            {
                VoltageBox.Select(VoltageBox.Text.Length, 0);
            }
        }
    }

    string currentText = "0 V";
    public string CurrentText
    {
        get
        {
            return currentText;
        }

        set
        {
            currentText = value;
            OnPropertyChanged("CurrentText");
        }
    }     

您可以在启动时注意到Currenttext = 0 V并通过执行这些语句我想更改的值CurrentText

double tempval = 120.0;

string CurrentVal = Convert.ToString(tempval / 10);
CurrentText = CurrentVal; // here V as volts must be concatenated and value must be 12.0 V
4

1 回答 1

3

这是关于如何做事的粗略方法。

首先在您的 ViewModel 上,有一个属性是您的电压有效值,还有一个计算属性会添加一个“V”。做这些事情是 ViewModel 的责任,而不是 View 的责任。

    private double voltage = 0.0D;
    public double Voltage
    {
        get
        {
            return voltage;
        }

        set
        {
            if (value > 5.0D || value < 0.0D)
                throw new InvalidOperationException();
            voltage = value;
            OnPropertyChanged("Voltage");
        }
    }

    public string VoltageText
    {
        get
        {
            return Voltage + " V";
        }
    }

然后为您的控制,不要使用 TextChanged,更喜欢使用 PreviewTextInput

在你的 waml 中:

    <TextBox Grid.Column="1" Text="{Binding Voltage}" Name="VoltageBox" PreviewTextInput="TextBox_PreviewTextInput" />
    <Label Grid.Column="2" Content="{Binding VoltageText}" Name="CurrentLabel" />

在后面的代码中

    private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        var textBox = sender as TextBox;
        if (textBox == null)
            throw new ArgumentException("sender");

        double result;
        var insertedText = e.Text;
        if (!double.TryParse(insertedText, out result) && insertedText != ".")
        {
            e.Handled = true;
        }
        else
        {
            string textboxCurrentValue = textBox.Text;
            int selectionLength = textBox.SelectionLength;
            int selectionStart = textBox.SelectionStart;
            if (selectionLength != 0)
            {
                textboxCurrentValue = textboxCurrentValue.Remove(selectionStart, selectionLength);
            }
            textboxCurrentValue = textboxCurrentValue.Insert(selectionStart, insertedText);

            if (!double.TryParse(textboxCurrentValue, out result) || result > 5.0D || result < 0.0D)
            {
                e.Handled = true;
            }
        }
    }

这应该让你开始!

于 2012-10-16T12:59:02.730 回答