2017-06-15 92 views
0

我不是很清楚wpf中的驗證規則是如何工作的。看起來我錯過了很多。我正在關注一個教程,對TextBox進行「Textbox is required」驗證。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); 
     } 
    } 

說實話我不確定我在驗證部分做了什麼,我只是想讓它工作。

+0

http://www.c-sharpcorner.com/blogs/wpf-required-field- validation1這是我試圖模擬的教程 – user3382285

回答

1

您需要將TextBoxText屬性綁定到驗證工作的源屬性。

Department類應該有一個string財產,讓我們稱呼它Name,該TextBox結合:

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; 
+0

謝謝你的答案。我可以使用控制器TextBox中的textBox.Text作爲綁定屬性嗎? – user3382285

+0

什麼是「控制器」文本框? – mm8

+0

像在TextBox中一樣textBox = new TextBox();然後我有textBox.Text – user3382285