私はWPFを初めて使用しており、バインドされたコレクションの検証に問題があります。バインディングコレクションのバインディングバリデーションを実装する方法
私はグループ化された多相オブジェクトのコレクションにバインドされたDataGridを持っています。 Lableワンタイムは、テキストボックスのLostFocus
データグリッドの各ラインがに制限される場合
- :
データグリッドは、2つの列を有しています基底クラスからの1つのオブジェクト。各派生クラスには独自の検証メソッドがあり、派生クラスのインスタンスに基づいてTextBoxの適切な検証メソッドを実行しようとしています。
私はExceptionValidationRuleに基づいて検証をバインドできますが、パフォーマンスはあまり良くなく、アプリケーションは多くの例外を含んでいるため点滅します。また、TextBox LostFocusのイベントをスローしてソースオブジェクトを取得することもできますが、可能な場合はバインドされたオブジェクトに対してWPF検証を使用しようとしています。
基本クラスのValidationRuleを継承して渡そうとしましたが、基底クラスで関数には入力されましたが、派生した限定アイテムではありません ValidationRuleとDependencyObjectを実装する別のクラスを作成しようとしました。 http://dedjo.blogspot.com/2007/05/fully-binded-validation-by-using.html
しかし、私はまだそれを動作させることができません。簡単にするために、私はTestStringプロパティを持つDependencyObjectを作成し、それを表示名(Labelの束縛されたパス)にバインドしようとしました。しかし、TestStringのセットは決して呼び出されません。
どうすればいいですか?適切な検証方法をどのように呼び出すことができますか?限られたオブジェクトを何らかの方法でValuatorに渡すことはできますか?あなたの助けのための
おかげで、 サリット
私が使用するコード:
たDependencyObjectの実装クラス:のValidationRuleの
class ManifestPropertyDependency : DependencyObject { private ManifestProperty _manifestPropertyInstance; public ManifestProperty ManifestPropertyInstance { get { return (ManifestProperty)GetValue(ManifestPropertyInstanceProp); } set { SetValue(ManifestPropertyInstanceProp, value); } } private string testString; public string TestString { get { return (string)GetValue(TestStringProp); } set { SetValue(TestStringProp, value); } } public ManifestPropertyDependency() { testString = ""; _manifestPropertyInstance = new ManifestProperty(); } public static readonly DependencyProperty ManifestPropertyInstanceProp = DependencyProperty.Register("ManifestPropertyInstance", typeof(ManifestProperty), typeof(ManifestPropertyDependency), new UIPropertyMetadata(new ManifestProperty())); public static readonly DependencyProperty TestStringProp = DependencyProperty.Register("TestString", typeof(string), typeof(ManifestPropertyDependency), new UIPropertyMetadata("")); }
実装クラス:
class ManifestPropertyValidator : ValidationRule { private ManifestProperty _manifest; private ManifestPropertyDependency _manifestPropertyDependency; public string Stam { get { return _manifestPropertyDependency.TestString; } set { _manifestPropertyDependency.TestString = value; } } public ManifestPropertyDependency ManifestPropertyDependency { get { return _manifestPropertyDependency; } set { _manifestPropertyDependency = value; Stam = value.TestString; _manifest = value.ManifestPropertyInstance; } } public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { try { string errorMessage = ""; if (ManifestPropertyDependency.ManifestPropertyInstance.ValidateString((string)value, ref errorMessage)) { return new ValidationResult(true, null); } return new ValidationResult(false, errorMessage); } catch (Exception e) { return new ValidationResult(false, "Illegal characters or " + e.Message); } } }
XAML検証:
<TextBox Validation.ErrorTemplate="{StaticResource validationTemplate}" Style="{StaticResource textStyleTextBox}"> <TextBox.Text> <Binding Path="Value" Mode="TwoWay" UpdateSourceTrigger="LostFocus"> <Binding.ValidationRules> <Classes:ManifestPropertyValidator> <Classes:ManifestPropertyValidator.ManifestPropertyDependency> <Classes:ManifestPropertyDependency TestString="{Binding Path=DisplayName}"/> </Classes:ManifestPropertyValidator.ManifestPropertyDependency> </Classes:ManifestPropertyValidator> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox>
'ManifestPropertyDependency'クラスでは、依存関係プロパティのために' _manifestPropertyInstance'と 'testString'というバッキングフィールドを定義します。これは間違っています! DPには裏打ちフィールドは必要ありません。彼らのgetterとsetterは、代わりに 'DependencyObject.GetValue'と' DependencyObject.GetValue'を呼び出します。使用されていないバッキングフィールドに値を割り当てることは、コンストラクタの場合と同様、無意味です。代わりにプロパティを設定します。また、XAMLでこれらのDPを使用すると、WPF **はゲッターやセッターを呼び出すのではなく、基になるDPに直接アクセスします。 – Clemens
私はバッキングフィールドがネセサリーではないことを知っています。それはデバッグのためだけのことでした。私はDependencyObjectで何が起こっているのかを理解しようとしました。 DependencyObjectの取得と設定はManifestPropertyValidatorから呼び出されなければなりません。このフィールドの中に何があるのかを確認しようとしました。 – user1197547