簡體   English   中英

WPF動態文本框驗證

[英]WPF dynamic textboxes validation

我不太清楚wpf中的驗證規則如何工作,似乎我錯過了很多東西。 我正在關注有關對TextBox進行“文本框必需”驗證的教程。

本教程已在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);
        }
    }

老實說,我不確定自己在驗證部分中正在做什么,而我只是想使其起作用。

您需要將TextBoxText屬性綁定到source屬性,以使驗證生效。

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM