【问题标题】:WPF dynamic textboxes validationWPF 动态文本框验证
【发布时间】:2017-06-16 22:19:18
【问题描述】:

我不太清楚 wpf 中的验证规则是如何工作的。看来我错过了很多。我正在关注有关对文本框进行“需要文本框”验证的教程。

本教程已在 xaml 上完成。但是,我在后面的代码(C#)中创建了动态文本框。

在 Xaml 中我添加了以下资源

<UserControl.Resources>
    <ControlTemplate x:Key="validationTemplate">
        <DockPanel>
            <TextBlock Foreground="Red" FontSize="25" Text="*" DockPanel.Dock="Right"></TextBlock>
            <AdornedElementPlaceholder/>
        </DockPanel>
    </ControlTemplate>

    <Style x:Key="InputControlErrors" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"    
          Value="{Binding RelativeSource={x:Static RelativeSource.Self},    
                          Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

另外,我添加了验证类

 public class RequiredFiedValidationRule : ValidationRule {
 public RequiredFiedValidationRule() {

  }
  public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
      if (value.ToString().Length > 0) {
          return new ValidationResult(true, null);
      }
      else {
          return new ValidationResult(false, "Required Field Validation");
      }
  }
}

在我的这部分代码中,我在 for 循环中生成文本框。本教程在 Xaml 中完成了所有绑定,在创建每个 TextBox 期间,我尝试在我的代码中做同样的事情。代码运行时没有错误,但在验证部分没有结果(它仍然接受空值)。注意:这只是for循环中的一部分代码,我删除了不重要的部分。

 foreach (Department department in Departments) { 
            TextBox textBox = new TextBox();
            textBox.Name = "Department" + department.Id.ToString();
            textBox.Width = 70;
            textBox.MaxWidth = 70;
            textBox.HorizontalAlignment = HorizontalAlignment.Center;
            textBox.VerticalAlignment = VerticalAlignment.Center;
            Grid.SetColumn(textBox, 1);
            Grid.SetRow(textBox, row);

            //me trying to attache the validation rule with no luck :(

            Validation.SetErrorTemplate(textBox, this.FindResource("validationTemplate") as ControlTemplate);
            textBox.Style= this.FindResource("InputControlErrors") as Style;
            Binding binding = new Binding();
            binding.Source = this;
            binding.Path = new PropertyPath(textBox.Name);
            binding.Mode = BindingMode.TwoWay;
            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            RequiredFiedValidationRule textBoxRequiredRule = new RequiredFiedValidationRule();
            binding.ValidationRules.Add(textBoxRequiredRule);
            BindingOperations.SetBinding(textBox, TextBox.TextProperty, binding);
            NCRGrid.Children.Add(textBox);
        }
    }

老实说,我不确定自己在验证部分做了什么,我只是想让它发挥作用。

【问题讨论】:

标签: c# wpf xaml


【解决方案1】:

您需要将TextBoxText 属性绑定到源属性才能进行验证。

Department 类应该有一个string 属性,我们称之为NameTextBox 绑定到:

Binding binding = new Binding();
binding.Source = department; // <--
binding.Path = new PropertyPath("Name"); //<-- "Name" refers to a property of the Department class
binding.Mode = BindingMode.TwoWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

【讨论】:

  • 感谢您的回答。我可以使用控制器 TextBox 中的 textBox.Text 作为绑定属性吗?
  • 什么是“控制器”文本框?
  • 像在文本框中一样 textBox = new TextBox();然后我有 textBox.Text
  • TextBox.Text 是 target 属性。正如我在回答中所说,您需要将其绑定到源属性。您不能将其绑定到自身。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-21
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-08
  • 2011-02-10
相关资源
最近更新 更多