• <legend id='CYPLv'><style id='CYPLv'><dir id='CYPLv'><q id='CYPLv'></q></dir></style></legend>
  • <small id='CYPLv'></small><noframes id='CYPLv'>

      <i id='CYPLv'><tr id='CYPLv'><dt id='CYPLv'><q id='CYPLv'><span id='CYPLv'><b id='CYPLv'><form id='CYPLv'><ins id='CYPLv'></ins><ul id='CYPLv'></ul><sub id='CYPLv'></sub></form><legend id='CYPLv'></legend><bdo id='CYPLv'><pre id='CYPLv'><center id='CYPLv'></center></pre></bdo></b><th id='CYPLv'></th></span></q></dt></tr></i><div id='CYPLv'><tfoot id='CYPLv'></tfoot><dl id='CYPLv'><fieldset id='CYPLv'></fieldset></dl></div>
      <tfoot id='CYPLv'></tfoot>

          <bdo id='CYPLv'></bdo><ul id='CYPLv'></ul>

      1. WPF 文本框的验证规则

        时间:2023-06-09
        <i id='bTazP'><tr id='bTazP'><dt id='bTazP'><q id='bTazP'><span id='bTazP'><b id='bTazP'><form id='bTazP'><ins id='bTazP'></ins><ul id='bTazP'></ul><sub id='bTazP'></sub></form><legend id='bTazP'></legend><bdo id='bTazP'><pre id='bTazP'><center id='bTazP'></center></pre></bdo></b><th id='bTazP'></th></span></q></dt></tr></i><div id='bTazP'><tfoot id='bTazP'></tfoot><dl id='bTazP'><fieldset id='bTazP'></fieldset></dl></div>
        • <small id='bTazP'></small><noframes id='bTazP'>

            <tbody id='bTazP'></tbody>
          <legend id='bTazP'><style id='bTazP'><dir id='bTazP'><q id='bTazP'></q></dir></style></legend>

            <tfoot id='bTazP'></tfoot>

              <bdo id='bTazP'></bdo><ul id='bTazP'></ul>

                • 本文介绍了WPF 文本框的验证规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 WPF 的新手.在我的 UserControl 中,我有 8 个标签及其各自的 8 个文本框,如下所示:

                  I am newbie to WPF.In my UserControl,I have 8 labels and its respective 8 textboxes as follows:

                  1.Label : abc   2.Label : def
                    TextBox1 :        TextBox2 :
                  
                  3.Label :xyz    4. Label : ghi
                    Textbox3 :        TextBox4 :
                  

                  每个文本框文本属性都应包含以其各自标签名称结尾的文本对于 TextBox1.text 应该是 xxxx.abcTextBox2.text 应该是 xxxx.def 等等.如果不是,文本框应该有红色边框.

                  Each of these textbox text property should contain text ending with its respective label name for TextBox1.text should be xxxx.abc, TextBox2.text should be xxxx.def and so on.if not textbox should have red border.

                  希望我对细节很清楚.所以我需要为每个文本框编写不同的 ValidationRule 吗??

                  hope I am clear with the details.So Do i need to write different ValidationRule for each textbox??

                  你有什么意见吗??

                  推荐答案

                  为什么不拥有一个 ValidationRule 实现,用一个属性公开字段应该以什么结尾,例如:

                  Why not have one ValidationRule implementation, with a property exposing what the field should end with, e.g:

                  public class EndsWithValidationRule : ValidationRule
                  {
                      public string MustEndWith { get; set; }
                  
                      public override ValidationResult Validate(object value, CultureInfo cultureInfo)
                      {
                          var str = value as string;
                          if(str == null)
                          {
                              return new ValidationResult(false, "Please enter some text");
                          }
                          if(!str.EndsWith(MustEndWith))
                          {
                              return new ValidationResult(false, String.Format("Text must end with '{0}'", MustEndWith));
                          }
                          return new ValidationResult(true, null);
                  
                      }
                  }
                  

                  然后你可以这样使用:

                  <TextBox x:Name="TextBox1">
                      <TextBox.Text>
                          <Binding Path="BoundProperty1" UpdateSourceTrigger="PropertyChanged">
                              <Binding.ValidationRules>
                                  <local:EndsWithValidationRule MustEndWith=".def" />
                              </Binding.ValidationRules>
                          </Binding>
                      </TextBox.Text>
                  </TextBox>
                  
                  <TextBox x:Name="TextBox2">
                      <TextBox.Text>
                          <Binding Path="BoundProperty2" UpdateSourceTrigger="PropertyChanged">
                              <Binding.ValidationRules>
                                  <local:EndsWithValidationRule MustEndWith=".abc" />
                              </Binding.ValidationRules>
                          </Binding>
                      </TextBox.Text>
                  </TextBox>
                  

                  这篇关于WPF 文本框的验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:访问中继器控件中的文本框 下一篇:从动态添加的文本框中获取值 asp.net c#

                  相关文章

                  <small id='jZ365'></small><noframes id='jZ365'>

                • <legend id='jZ365'><style id='jZ365'><dir id='jZ365'><q id='jZ365'></q></dir></style></legend>
                  • <bdo id='jZ365'></bdo><ul id='jZ365'></ul>

                  <i id='jZ365'><tr id='jZ365'><dt id='jZ365'><q id='jZ365'><span id='jZ365'><b id='jZ365'><form id='jZ365'><ins id='jZ365'></ins><ul id='jZ365'></ul><sub id='jZ365'></sub></form><legend id='jZ365'></legend><bdo id='jZ365'><pre id='jZ365'><center id='jZ365'></center></pre></bdo></b><th id='jZ365'></th></span></q></dt></tr></i><div id='jZ365'><tfoot id='jZ365'></tfoot><dl id='jZ365'><fieldset id='jZ365'></fieldset></dl></div>
                  1. <tfoot id='jZ365'></tfoot>