2010-11-28 18 views
1

xamlのValidationRuleのxamlの添付プロパティまたは依存プロパティをバインドし、次に添付プロパティまたは依存プロパティの値に基づいて検証ルールで合計判定を行いたい。私は解決策を見つけることができません バリデーションルールにバインド可能な値を渡す方法ValidationRule WPFに添付または依存するプロパティ

+1

具体的に達成しようとしていること(インプット、期待されるアウトプットなど)についてさらに情報を提供できますか? – Moonshield

答えて

2

私はあなたに役立つサンプルコードを提供します。私はtexboxユーザ入力を検証するためにValidationRuleを定義しました。検証のタイプは、1つのenumパラメータの値に従って実行されます。利用可能な検証のタイプは、ユーザー入力を空にすることはできません。ユーザー入力は数字でなければならず、ユーザー入力はIPアドレスでなければなりません。第2のパラメータは、特定の警告メッセージを表示することを可能にする。あなたが知っているように、バインディング目的の変数はDependendyPropertyでなければならないので、ここではパラメタ宣言を持つクラスを見つける。

public class ValidationParams : DependencyObject 
{ 
    // Dependency Properties 
    public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", 
                          typeof(string), 
                          typeof(ValidationParams), 
                          new FrameworkPropertyMetadata(string.Empty)); 

    public static readonly DependencyProperty ValidationTypeProperty = DependencyProperty.Register("ValidationType", 
                          typeof(FieldValidationRule.EnmValidationType), 
                          typeof(ValidationParams), 
                          new FrameworkPropertyMetadata(FieldValidationRule.EnmValidationType.FieldNotEmpty)); 

    // Properties 
    [Category("Message")] 
    public string Message 
    { 
     get { return (string)GetValue(MessageProperty); } 
     set { SetValue(MessageProperty, value); } 
    } 

    [Category("ValidationType")] 
    public FieldValidationRule.EnmValidationType ValidationType 
    { 
     get { return (FieldValidationRule.EnmValidationType)GetValue(ValidationTypeProperty); } 
     set { SetValue(ValidationTypeProperty, value); } 
    } 

そして、ここでのValidationRuleクラスです:あなたは

     <TextBox Style="{DynamicResource FieldValue}" Grid.Column="1" IsReadOnly="False"> 
         <TextBox.Text> 
          <Binding Source="{StaticResource XmlItemChannel}" XPath="@Name" Mode="TwoWay" UpdateSourceTrigger="LostFocus"> 
           <Binding.ValidationRules> 
            <data:FieldValidationRule> 
             <data:FieldValidationRule.Params> 
              <data:ValidationParams Message="{DynamicResource ERR002}" ValidationType="FieldNotEmpty" /> 
             </data:FieldValidationRule.Params> 
            </data:FieldValidationRule> 
           </Binding.ValidationRules> 
          </Binding> 
         </TextBox.Text> 
        </TextBox> 

あなたは、パラメータのメッセージがリソースにバインドされていることを確認することができますが、:

public class FieldValidationRule : ValidationRule 
{ 
    public enum EnmValidationType 
    { 
     FieldNotEmpty, 
     FieldNumeric, 
     FieldIPAddress 
    } 

    // Local variables and objects 
    private ValidationParams mParams = new ValidationParams(); 

    public ValidationParams Params 
    { 
     get { return mParams; } 
     set { mParams = value; } 
    } 

    // Override 
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) 
    { 
     ValidationResult objResult = null; 
     string sValue = value as string; 
     objResult = new ValidationResult(true, null); 
     switch (Params.ValidationType) 
     { 
      case EnmValidationType.FieldNotEmpty: 
       if(string.IsNullOrEmpty(sValue) == true) 
        objResult = new ValidationResult(false, Params.Message); 
       break; 
      case EnmValidationType.FieldNumeric: 
       int iValue = 0; 
       if(int.TryParse(sValue, out iValue) == false) 
        objResult = new ValidationResult(false, Params.Message); 
       break; 
      case EnmValidationType.FieldIPAddress: 
       IPAddress objValue = IPMatrix.CreateHostAddr(); 
       if(IPAddress.TryParse(sValue, out objValue) == false) 
        objResult = new ValidationResult(false, Params.Message); 
       break; 
     } 
     return objResult; 
    } 
} 

そして最後に、ここでは、XAMLコードであります古典的にもそれを束縛することができます。

+0

良い仕事ですが、バインドしようとすると、 '対象の要素の統治FrameworkElementまたはFrameworkContentElementが見つかりません'というバインディングエラーが発生します。 Thomas Levesqueはこれに対する答えを持っています。http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/を参照してください。 –