本文介绍了我想将我的文本框输入限制为0-4数字和? ,FIB,TMC字符串。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一些字符串与texboxes绑定,但我也启用了文本框的手动文本输入,但我希望文本框只接受输入whch是给定列表中的匹配我应该做什么.. plz help 我尝试过: xaml代码: < textbox grid.column = 1 grid.row = 5 textalignment = 中心 verticalalignment = Bottom horizontalalignment = 中心 text = {Binding Path = ResultModelObject.Grades [0],Mode = TwoWay,UpdateSourceTrigger = PropertyChanged} width = 60 maxlength = 3 可见性 = {Binding Path = ResultModelObject.VisibilityOfColumns [0],Mode = OneWay,UpdateSourceTrigger = PropertyCha nged} > < textbox.inputbindings > < mousebinding 手势 = LeftClick command = {Binding Path = ChangeGradeCommand} commandparameter = 0 / > < / textbox.inputbindings > < / textbox > < textbox grid.column = 2 grid.row = 5 textalignment = 中心 verticalalignment = 底部 horizontalalignment = 中心 text = {Binding Path = ResultModelObject.Grades [1],Mode = TwoWay,UpdateSourceTrigger = PropertyChanged} width = 60 maxlength = 3 visibility = {Binding Path = ResultModelObject.VisibilityOfColumns [1],Mode = OneWay,UpdateSourceTrigger = PropertyChanged} > < textbox.inputbindings > < mousebinding 手势 = LeftClick command = {Binding Path = ChangeGradeCommand} commandparameter = 1 / > < / textbox.inputbindings > < / textbox > < textbox grid.column = 3 grid.row = 5 textalignment = 中心 verticalalignment = 底部 horizontalalignment = 中心 text = {Binding Path = ResultModelObject.Grades [2],Mode = TwoWay,UpdateSourceTrigger = PropertyChanged} width = 60 maxlength = 3 可见性 = {Binding Path = ResultModelObject.VisibilityOfColumns [2],Mode = OneWay,UpdateSourceTrigger = PropertyChanged} > < textbox.inputbindings > < mousebinding 手势 = LeftClick 命令 = {Binding Path = ChangeGradeCommand} commandparameter = 2 / > < / textbox。化InputBindings > < / textbox > < 文本框 grid.column = 4 grid.row = 5 textalignment = 中心 verticalalignment = 底部 horizontalalignment = 中心 text = {Binding Path = ResultModelObject.Grades [3],Mode = TwoWay,UpdateSourceTrigger = PropertyChanged} width = 60 maxlength = 3 可见性 = {Binding Path = ResultModelObject。 VisibilityOfColumns [3],Mode = OneWay,UpdateSourceTrigger = PropertyChanged} > & textbox.inputbindings > < mousebinding gesture = LeftClick command = {Binding Path = ChangeGradeCommand} commandparameter = 3 / > < / textbox.inputbindings > ; < / textbox > < textbox grid.column = 5 grid.row = 5 textalignment = 中心 verticalalignment = 底部 horizontalalignment = 中心 text = {Binding Path = ResultModelObject.Grades [4],Mode = TwoWay, UpdateSourceTrigger = PropertyChanged} width = 60 maxlength = 3 可见性 = {Binding Path = ResultModelObject.VisibilityOfColumns [4],Mode = OneWay ,UpdateSourceTrigger = PropertyChanged} > < textbox.inputbindings > < mousebinding 手势 = LeftClick command = {Binding Path = ChangeGradeCommand} commandparameter = 4 / > < / textbox.inputbindings > < / textbox > < textbox grid.column = 6 grid.row = 5 textalignment = 中心 verticalalignment = 底部 horizontalalignment = 中心 text = {Binding Path = ResultModelObject.Grades [5],Mode = TwoWay,UpdateSourceTrigger = PropertyChanged} width = 60 maxlength = 3 visibility = {Binding Path = ResultModelObject.VisibilityOfColumns [5],Mode = OneWay,UpdateSourceTrigger = PropertyChanged} > < textbox.inputbindings > < mousebinding 手势 = LeftClick command = {Binding Path = ChangeGradeCommand} commandparameter = 5 / > < ; / textbox.inputbindings > < / textbox > cs文件: public ObservableCollection< string>成绩 { 获取 { 返回 m_Grades ; } set { switch ( value .ToString()) { case 0: m_Grades = value ; break ; case 1 : m_Grades = value ; break ; case 2 : m_Grades = value ; break ; case 3 : m_Grades = value ; break ; case 4 : m_Grades = value ; break ; case 0.5 : m_Grades = value ; break ; case FC : m_Grades = value ; break ; case TMC : m_Grades = value ; break ; case MF : m_Grades = value ; break ; case ? : m_Grades = value ; break ; } 如果(m_Grades!= value ) { m_Grades = value ; OnPropertyChanged( Grades); } } } public 事件 PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged( string propertyName) { if (PropertyChanged!= null ) { PropertyChanged( this , new PropertyChangedEventArgs(propertyName)); } } 解决方案 这就是使用适当的控制以捕获用户输入。 例如: 如果你有固定的选项列表(例如1月,2月,......,12月)然后使用仅向用户呈现该可能性列表的控件。 ComboBox控件 [ ^ ]就是一个很好的例子。正如@SinisaHajnal在评论中建议的那样,一套 RadioButton控件 [ ^ ]将是另一个不错的选择。 如果你有多种选择可以组合(例如,房子必须有一个花园和停车场并且可以分离)然后一套 CheckBox控件 [ ^ ]是理想的。 要选择范围内的整数值,请考虑使用滑块控件 [ ^ ] 如果是您想要的日期,请使用 DatePicker [ ^ ] 只有当你需要完全自由格式的输入时才需要使用TextBox(例如名称,地址,评论) 这些指南不仅适用于WPF或WinForms - 它们适用于任何试图从人类捕获数据的内容。目的是让用户尽可能轻松地输入正确的数据,并在这样做时获得愉快的体验。 从您的描述中问题我建议使用ComboBox i have array of strings that bind with texboxes but i also enable manual text input to the textboxes but i want textboxes only accepts input whch is matches in given list what shoulid i do it.. plz helpWhat I have tried:xaml code:<textbox grid.column="1" grid.row="5" textalignment="Center" verticalalignment="Bottom" horizontalalignment="Center" text="{Binding Path=ResultModelObject.Grades[0], Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" width="60" maxlength="3" visibility="{Binding Path=ResultModelObject.VisibilityOfColumns[0], Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"> <textbox.inputbindings> <mousebinding gesture="LeftClick" command="{Binding Path=ChangeGradeCommand}" commandparameter="0" /> </textbox.inputbindings> </textbox> <textbox grid.column="2" grid.row="5" textalignment="Center" verticalalignment="Bottom" horizontalalignment="Center" text="{Binding Path=ResultModelObject.Grades[1], Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" width="60" maxlength="3" visibility="{Binding Path=ResultModelObject.VisibilityOfColumns[1], Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"> <textbox.inputbindings> <mousebinding gesture="LeftClick" command="{Binding Path=ChangeGradeCommand}" commandparameter="1" /> </textbox.inputbindings> </textbox> <textbox grid.column="3" grid.row="5" textalignment="Center" verticalalignment="Bottom" horizontalalignment="Center" text="{Binding Path=ResultModelObject.Grades[2], Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" width="60" maxlength="3" visibility="{Binding Path=ResultModelObject.VisibilityOfColumns[2], Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"> <textbox.inputbindings> <mousebinding gesture="LeftClick" command="{Binding Path=ChangeGradeCommand}" commandparameter="2" /> </textbox.inputbindings> </textbox> <textbox grid.column="4" grid.row="5" textalignment="Center" verticalalignment="Bottom" horizontalalignment="Center" text="{Binding Path=ResultModelObject.Grades[3], Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" width="60" maxlength="3" visibility="{Binding Path=ResultModelObject.VisibilityOfColumns[3], Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"> <textbox.inputbindings> <mousebinding gesture="LeftClick" command="{Binding Path=ChangeGradeCommand}" commandparameter="3" /> </textbox.inputbindings> </textbox> <textbox grid.column="5" grid.row="5" textalignment="Center" verticalalignment="Bottom" horizontalalignment="Center" text="{Binding Path=ResultModelObject.Grades[4], Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" width="60" maxlength="3" visibility="{Binding Path=ResultModelObject.VisibilityOfColumns[4], Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"> <textbox.inputbindings> <mousebinding gesture="LeftClick" command="{Binding Path=ChangeGradeCommand}" commandparameter="4" /> </textbox.inputbindings> </textbox> <textbox grid.column="6" grid.row="5" textalignment="Center" verticalalignment="Bottom" horizontalalignment="Center" text="{Binding Path=ResultModelObject.Grades[5], Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" width="60" maxlength="3" visibility="{Binding Path=ResultModelObject.VisibilityOfColumns[5], Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"> <textbox.inputbindings> <mousebinding gesture="LeftClick" command="{Binding Path=ChangeGradeCommand}" commandparameter="5" /> </textbox.inputbindings> </textbox>cs file:public ObservableCollection<string> Grades { get { return m_Grades; } set { switch (value.ToString()) { case "0": m_Grades = value; break; case "1": m_Grades = value; break; case "2": m_Grades = value; break; case "3": m_Grades = value; break; case "4": m_Grades = value; break; case "0.5": m_Grades = value; break; case "FC": m_Grades = value; break; case "TMC": m_Grades = value; break; case "MF": m_Grades = value; break; case "?": m_Grades = value; break; } if (m_Grades != value) { m_Grades = value; OnPropertyChanged("Grades"); } } } public event PropertyChangedEventHandler PropertyChanged;protected void OnPropertyChanged(string propertyName){ if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }} 解决方案 This is all about using the appropriate control to capture user input.For example:If you have a fixed list of options (e.g. "January", "February", ... , "December") then use a control that only presents that list of possibilities to the user. The ComboBox control[^] is a good example of this. As @SinisaHajnal has suggested in the comments, a set of RadioButton controls[^] would be another good alternative.If you have a variety of options that can be combined (E.g. The House must have a Garden AND Parking AND be Detached) then a set of CheckBox controls[^] would be ideal.For selecting an integer value within a range consider using a Slider Control[^]If it's dates you want then use a DatePicker[^]It's only when you want completely free-form input that you should need to use a TextBox (e.g. Names, Addresses, Comments)These guidelines don't just apply to WPF or WinForms - they apply to anything that attempts to capture data from a human. The object is to make it as easy as possible for a User to enter the correct data and to have a pleasant "experience" whilst doing so.From your description of the problem I would suggest using a ComboBox 这篇关于我想将我的文本框输入限制为0-4数字和? ,FIB,TMC字符串。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 09:03