2009-09-28 4 views
5

バインディング部分なしで検証を使用することは可能ですか?私のテキストボックスはどのオブジェクトにもバインドされていませんが、それでもその内容を検証したいのです。私がこれまでに見つけた唯一の方法はこれです:WPFバインドされていないテキストボックスを検証する

<TextBox Grid.Row="0" Grid.Column="1" MaxLength="50" x:Name="textBoxTubeName" Margin="5,5,0,5"> 
     <TextBox.Text> 
      <Binding Path="Name" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" NotifyOnValidationError="True"> 
       <Binding.ValidationRules> 
        <validation:InvalidCharactersRule /> 
       </Binding.ValidationRules> 
      </Binding> 
     </TextBox.Text> 
    </TextBox> 

しかしTextBox.Text(この場合は、Nameプロパティ)何かにバインドされたときに、再び、それだけで動作しますが、どのように私はこれに行きますか縛らずに?

ありがとうございます!

答えて

3

According to the MSDN forumsまだできませんが、計画中です(注:これは古い投稿です)。しかし、私はまだそれを実行する方法を見つけることができないので、まだ実装されていない可能性があります。

+0

ええ、私はコードを自分で行う必要があると思います。私はバインディングに集中するマイクロソフトのアイデアは良いと思うが、その目的のためにバリデーションをこのように制限することはあまり良くない。 Lucasに感謝します。 – Carlo

+0

@Carlo:完全に合意しました。 – Kredns

+0

こんにちは。この件に関する最新情報はありますか?私は同じことをしたいと思います。 –

2

コードビハインドからやるのは難しいことです。基本的には、コードから一時バインディングを設定し、検証エラーを発生させることができます。入力が有効な値を持つ場合、一時バインディングのすべてを再度削除できます。私は(それは何もないから良いでしょう)悪い習慣を考慮して、私が使用しているもの。ここ

、:

/// <summary> 
    /// Marks a textBox control as invalid (via validation error) from code. 
    /// </summary> 
    /// <param name="textBox">The text box.</param> 
    /// <param name="errorContent">Content of the error.</param> 
    public static void ValidationMarkInvalid(TextBox textBox, String errorContent) 
    { 
     DependencyProperty textProp = TextBox.TextProperty; 
     if (!BindingOperations.IsDataBound(textBox, textProp)) 
     { 
      if (textBox.DataContext == null) 
      { 
       textBox.DataContext = new EmptyDataContext(); 
      } 

      Binding b = new Binding("CodeBehind"); 
      b.FallbackValue = textBox.Text; 
      b.ValidatesOnExceptions = true; 
      BindingOperations.SetBinding(textBox, textProp, b); 
     } 

     BindingExpression bindingInError = 
      textBox.GetBindingExpression(TextBox.TextProperty); 

     var validationError = new ValidationError(
      new EmptyValidationRule(), 
      bindingInError, 
      errorContent, 
      new Exception(errorContent)); 

     Validation.MarkInvalid(bindingInError, validationError); 
    } 

    /// <summary> 
    /// Clears the validation error from a textBox. 
    /// </summary> 
    /// <param name="textBox">The text box.</param> 
    public static void ValidationClear(TextBox textBox) 
    { 
     DependencyProperty textProp = TextBox.TextProperty; 
     if (BindingOperations.IsDataBound(textBox, textProp)) 
     { 
      String value = textBox.Text; 
      Validation.ClearInvalid(textBox.GetBindingExpression(TextBox.TextProperty)); 
      BindingOperations.ClearBinding(textBox, textProp); 
      textBox.Text = value; 

      EmptyDataContext ctx = textBox.DataContext as EmptyDataContext; 
      if (ctx != null) 
      { 
       textBox.DataContext = null; 
      } 
     } 
    } 

    #region Nested Type: EmptyDataContext 
    private sealed class EmptyDataContext : INotifyPropertyChanged 
    { 
     public Object CodeBehind 
     { 
      get 
      { 
       throw new FormatException(); 
      } 
      set 
      { 
       throw new FormatException(); 
      } 
     } 

     #region INotifyPropertyChanged Members 
     public event PropertyChangedEventHandler PropertyChanged; 
     #endregion 
    } 
    #endregion 

    #region Nested Type: EmptyValidationRule 
    private sealed class EmptyValidationRule : ValidationRule 
    { 
     /// <summary> 
     /// When overridden in a derived class, performs validation checks on a value. 
     /// </summary> 
     /// <param name="value">The value from the binding target to check.</param> 
     /// <param name="cultureInfo">The culture to use in this rule.</param> 
     /// <returns> 
     /// A <see cref="T:System.Windows.Controls.ValidationResult"/> object. 
     /// </returns> 
     public override ValidationResult Validate(Object value, CultureInfo cultureInfo) 
     { 
      return new ValidationResult(false, String.Empty); 
     } 
    } 
    #endregion 

さて、コードビハインドあなたから、あなたはこれを行う:

ValidationMarkInvalid(textBox, "Please enter something valid!"); 

とに検証をクリア:

ValidationClear(textBox); 

PS:あなたはあなたができる例外に検証したくない場合EmptyDataContextクラスを上記のメソッドから削除します。

関連する問題