C# 未能设置文本按钮的输入限制&;将值与标签连接

C# 未能设置文本按钮的输入限制&;将值与标签连接,c#,.net,wpf,textbox,label,C#,.net,Wpf,Textbox,Label,我正在工作的WPF应用程序中,我有一个文本框,按钮,标签和切换按钮。在我的应用程序中,我应该执行以下两个操作: 仅将文本框的输入设置为0.0到5.0之间。因此,如果写入的任何值超过5.0,则必须限制为5。同样,写入0.0以下的任何值都必须受到限制,并且只能显示0.0 我的标签应将内容显示为“值”V。也就是说,如果值为3.4 V,则应将电压与其连接 下面是Xaml: <TextBox Grid.Column="1" Text="{Binding VoltageText}" Name="Vol

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

  • 仅将文本框的输入设置为0.0到5.0之间。因此,如果写入的任何值超过5.0,则必须限制为5。同样,写入0.0以下的任何值都必须受到限制,并且只能显示0.0

  • 我的标签应将内容显示为“值”V。也就是说,如果值为3.4 V,则应将电压与其连接

  • 下面是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");
            }
        }
    
        <TextBox Grid.Column="1" Text="{Binding Voltage}" Name="VoltageBox" PreviewTextInput="TextBox_PreviewTextInput" />
        <Label Grid.Column="2" Content="{Binding VoltageText}" Name="CurrentLabel" />
    
    这个文本框必须有我在第一点中提到的输入限制:)下面是我编写的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
    

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

    首先,在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";
            }
        }
    
    专用双电压=0.0D;
    公共双电压
    {
    得到
    {
    返回电压;
    }
    设置
    {
    如果(值>5.0D | |值<0.0D)
    抛出新的InvalidOperationException();
    电压=值;
    OnProperty改变(“电压”);
    }
    }
    公共字符串VoltageText
    {
    得到
    {
    返回电压+V;
    }
    }
    
    然后,对于您的控件,不要使用TextChanged,更喜欢使用previewtput

    在waml中:

    <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");
            }
        }
    
        <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;
                }
            }
        }
    
    private void TextBox\u预览输出(对象发送方,文本组合ventargs e)
    {
    var textBox=发送方作为textBox;
    if(textBox==null)
    抛出新的ArgumentException(“发送方”);
    双重结果;
    var insertedText=e.Text;
    如果(!double.TryParse(insertedText,out结果)&&insertedText!=“)
    {
    e、 已处理=正确;
    }
    其他的
    {
    字符串textboxCurrentValue=textBox.Text;
    int-selectionLength=textBox.selectionLength;
    int-selectionStart=textBox.selectionStart;
    如果(selectionLength!=0)
    {
    textboxCurrentValue=textboxCurrentValue.Remove(selectionStart,selectionLength);
    }
    textboxCurrentValue=textboxCurrentValue.Insert(选择开始,插入文本);
    如果(!double.TryParse(textboxCurrentValue,输出结果)| |结果>5.0D | |结果<0.0D)
    {
    e、 已处理=正确;
    }
    }
    }
    

    这应该让你开始

    下面是一个关于如何做事的粗略方法

    首先,在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";
            }
        }
    
    专用双电压=0.0D;
    公共双电压
    {
    得到
    {
    返回电压;
    }
    设置
    {
    如果(值>5.0D | |值<0.0D)
    抛出新的InvalidOperationException();
    电压=值;
    OnProperty改变(“电压”);
    }
    }
    公共字符串VoltageText
    {
    得到
    {
    返回电压+V;
    }
    }
    
    然后,对于您的控件,不要使用TextChanged,更喜欢使用previewtput

    在waml中:

    <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");
            }
        }
    
        <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;
                }
            }
        }
    
    private void TextBox\u预览输出(对象发送方,文本组合ventargs e)
    {
    var textBox=发送方作为textBox;
    if(textBox==null)
    抛出新的ArgumentException(“发送方”);
    双重结果;
    var insertedText=e.Text;
    如果(!double.TryParse(insertedText,out结果)&&insertedText!=“)
    {
    e、 已处理=正确;
    }
    其他的
    {
    字符串textboxCurrentValue=textBox.Text;
    int-selectionLength=textBox.selectionLength;
    int-selectionStart=textBox.selectionStart;
    如果(selectionLength!=0)
    {
    textboxCurrentValue=textboxCurrentValue.Remove(selectionStart,selectionLength);
    }
    textboxCurrentValue=textboxCurrentValue.Insert(选择开始,插入文本);
    如果(!double.TryParse(textboxCurrentValue,输出结果)| |结果>5.0D | |结果<0.0D)
    {
    e、 已处理=正确;
    }
    }
    }
    

    这应该让你开始

    你的问题到底是什么?你在寻找如何实现这一点的例子吗?@Niclas:我想将textbox的输入限制设置在0.0到5.0之间。在label中,我想连接一个字符串和值。您的问题是什么?你在寻找如何实现这一点的例子吗?@Niclas:我想将textbox的输入限制设置在0.0到5.0之间。在标签中,我想连接一个字符串和值。Thank mate:)它很有用Thank mate:)它很有用