質問

にコンポーネントのラインナップにセットアップできます検証に基づく誤差がスローされる個人データに層間のデータを結合を利用 ExceptionValidationRule または DataErrorValidationRule.

また束の制御の設定このように楽しむことができます。保存ボタンを押します。ユーザがクリックした際の飛びの保存ボタンを確認する必要がありまあ誤差の検証を行う前に、必ず保存します。がある場合の検証エラー、またholler生まれることを期待しています。

にコンポーネントのラインナップどうかという話があったようだ見合がつかない場合は、データの結合を制御して誤差の検証もしれません。

役に立ちましたか?

解決

この募集でしたがkm圏内にあります。下さった皆様、ありがとうござ寄与した。こちらはLINQのバージョンのお好き嫌いになりました。

private void CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = IsValid(sender as DependencyObject);
}

private bool IsValid(DependencyObject obj)
{
    // The dependency object is valid if it has no errors and all
    // of its children (that are dependency objects) are error-free.
    return !Validation.GetHasError(obj) &&
    LogicalTreeHelper.GetChildren(obj)
    .OfType<DependencyObject>()
    .All(IsValid);
}

他のヒント

次のコードからグコンポーネントのラインナップを予クリス-販売&Ian Griffiths)すべての検証結合ルールに依存オブジェクトとその子ども

public static class Validator
{

    public static bool IsValid(DependencyObject parent)
    {
        // Validate all the bindings on the parent
        bool valid = true;
        LocalValueEnumerator localValues = parent.GetLocalValueEnumerator();
        while (localValues.MoveNext())
        {
            LocalValueEntry entry = localValues.Current;
            if (BindingOperations.IsDataBound(parent, entry.Property))
            {
                Binding binding = BindingOperations.GetBinding(parent, entry.Property);
                foreach (ValidationRule rule in binding.ValidationRules)
                {
                    ValidationResult result = rule.Validate(parent.GetValue(entry.Property), null);
                    if (!result.IsValid)
                    {
                        BindingExpression expression = BindingOperations.GetBindingExpression(parent, entry.Property);
                        System.Windows.Controls.Validation.MarkInvalid(expression, new ValidationError(rule, expression, result.ErrorContent, null));
                        valid = false;
                    }
                }
            }
        }

        // Validate all the bindings on the children
        for (int i = 0; i != VisualTreeHelper.GetChildrenCount(parent); ++i)
        {
            DependencyObject child = VisualTreeHelper.GetChild(parent, i);
            if (!IsValid(child)) { valid = false; }
        }

        return valid;
    }

}

電話することを保存ボタンをクリックしイベントハンドラはこのようなページ/ウィンドウ

private void saveButton_Click(object sender, RoutedEventArgs e)
{

  if (Validator.IsValid(this)) // is valid
   {

    ....
   }
}

投稿されたコードは動作しなかった私にとっての使用ListBox.無事に卒業できた私は、しばらくするまでの作品:

public static bool IsValid(DependencyObject parent)
{
    if (Validation.GetHasError(parent))
        return false;

    // Validate all the bindings on the children
    for (int i = 0; i != VisualTreeHelper.GetChildrenCount(parent); ++i)
    {
        DependencyObject child = VisualTreeHelper.GetChild(parent, i);
        if (!IsValid(child)) { return false; }
    }

    return true;
}

同じ問題の提供ソリューション。組み合わせH-Man2のskiba_kのソリューションはほぼ微私にとって、例外:私のウィンドウをTabControl.の検証ルールにのみ評価され、TabItemは、現在見えています。私の交換VisualTreeHelperによるLogicalTreeHelper.今ことができる。

    public static bool IsValid(DependencyObject parent)
    {
        // Validate all the bindings on the parent
        bool valid = true;
        LocalValueEnumerator localValues = parent.GetLocalValueEnumerator();
        while (localValues.MoveNext())
        {
            LocalValueEntry entry = localValues.Current;
            if (BindingOperations.IsDataBound(parent, entry.Property))
            {
                Binding binding = BindingOperations.GetBinding(parent, entry.Property);
                if (binding.ValidationRules.Count > 0)
                {
                    BindingExpression expression = BindingOperations.GetBindingExpression(parent, entry.Property);
                    expression.UpdateSource();

                    if (expression.HasError)
                    {
                        valid = false;
                    }
                }
            }
        }

        // Validate all the bindings on the children
        System.Collections.IEnumerable children = LogicalTreeHelper.GetChildren(parent);
        foreach (object obj in children)
        {
            if (obj is DependencyObject)
            {
                DependencyObject child = (DependencyObject)obj;
                if (!IsValid(child)) { valid = false; }
            }
        }
        return valid;
    }

ほかのLINQを実行研究科長、楽しみいた包装のコードを拡張のためのDependencyObjects:

public static bool IsValid(this DependencyObject instance)
{
   // Validate recursivly
   return !Validation.GetHasError(instance) &&  LogicalTreeHelper.GetChildren(instance).OfType<DependencyObject>().All(child => child.IsValid());
}

これは何を考えreuseablity.

私は小さな最適化。

そうすれば多くの時間、同じコントロールを追加することができます上記のコードを保つのリストをコントロールを実際に検証している。その後必要なときにいつでもチェック有効性だけではなく、また、それらを管理ではなく、全体の視覚的ます。このことは証明されるかもしれない場合が多いのような制御できます。

こちらは 図書館 フォーム検証コンポーネントのラインナップ. Nugetパッケージこちら.

サンプル

<Border BorderBrush="{Binding Path=(validationScope:Scope.HasErrors),
                              Converter={local:BoolToBrushConverter},
                              ElementName=Form}"
        BorderThickness="1">
    <StackPanel x:Name="Form" validationScope:Scope.ForInputTypes="{x:Static validationScope:InputTypeCollection.Default}">
        <TextBox Text="{Binding SomeProperty}" />
        <TextBox Text="{Binding SomeOtherProperty}" />
    </StackPanel>
</Border>

の考え方を定義し、認証範囲の付属物件んの入力制御をします。それができる:

<ItemsControl ItemsSource="{Binding Path=(validationScope:Scope.Errors),
                                    ElementName=Form}">
    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="{x:Type ValidationError}">
            <TextBlock Foreground="Red"
                       Text="{Binding ErrorContent}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

きを反復すべての管理ツリーを再帰的にチェックを付着性バリデーションを実施します。HasErrorProperty、その後の最初だけます。

も利用できますその多くはすでに書き液 確認できます この スレッドの例と詳細情報

おに興味を持っているかもしれの BookLibrary サンプルアプリケーションの コンポーネントのラインナップアプリケーションフレームワーク(WAF).での使用方法検証コンポーネントのラインナップの保存ボタンを押すと検証の誤差が存在します。

回答形式aoganではなく、明示的に反復処理の検証ルール、より良いだけで呼び出し expression.UpdateSource():

if (BindingOperations.IsDataBound(parent, entry.Property))
{
    Binding binding = BindingOperations.GetBinding(parent, entry.Property);
    if (binding.ValidationRules.Count > 0)
    {
        BindingExpression expression 
            = BindingOperations.GetBindingExpression(parent, entry.Property);
        expression.UpdateSource();

        if (expression.HasError) valid = false;
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top