使用WPF的一些限制代码

限制textbox输入空格

//限制输入空格
            var input = txtgroup.Text.Trim();
            if (input.Length != 0)
            {
    
    
                double value;
                if (double.TryParse(input, out value))
                {
    
    
                    input = value.ToString();
                }
                else
                {
    
    
                    input = string.Empty;
                }
            }
            txtgroup.Text = input;

限制textbox只能输入数字

1.在TextBox中添加属性InputMethod.IsInputMethodEnabled=“False”
2.添加 PreviewTextInput="limitnumber"事件(要用到正则表达式)

public void limitnumber(object sender, TextCompositionEventArgs e)
        {
    
    
            Regex re = new Regex("[^0-9]+");
            e.Handled = re.IsMatch(e.Text);            
        }

限制textbox输入字符长度

限制textbox的输入字符长度只需要设置MaxLength属性,让他的值为你要设定的值就好了

设置ComboBox的默认值

可以设置默认选中项,设置它的selectedIndex=“0”.把第一项作为它的默认项。

再让第一项的visibility="collapsed"就好了

<ComboBoxItem Content="值日组" Visibility="Collapsed"></ComboBoxItem>


猜你喜欢

转载自blog.csdn.net/dingmengwei/article/details/114914947