3

データ検証を実装する方法を学ぼうとしていますが、最初の試行で lblSource_Error イベントが発生しません。誰かが私が逃したものを知っていますか?

私のウィンドウの XAML:

<Window x:Class="cCompleteWPFResourcesExamples.wValidationRule"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:cCompleteWPFResourcesExamples"
    Title="wValidationRule" Height="300" Width="300">
<Window.Resources>
    <local:Customer x:Key="rCustomer" Forename="InXaml" Surname="Created" ID="1"     
AmountOutstanding="0"/>
</Window.Resources>
<StackPanel x:Name="stkMain" DataContext="{StaticResource rCustomer}">
    <Label x:Name="lblSource" Validation.Error="lblSource_Error">
        <Label.Content>
            <Binding Path="ID" NotifyOnValidationError="True">
                <Binding.ValidationRules>
                    <local:cIDValidationRule/>
                </Binding.ValidationRules>
            </Binding>
        </Label.Content>
    </Label>
    <Label x:Name="lblErrorMessage" Content="No Error Yet"/>
</StackPanel>
</Window>

私のウィンドウのコード:

   namespace cCompleteWPFResourcesExamples
    {
    /// <summary>
    /// Interaction logic for wValidationRule.xaml
    /// </summary>
    public partial class wValidationRule : Window
    {
        Customer cus = new Customer();

        public wValidationRule()
        {
            InitializeComponent();
            cus.ID = 0;
            stkMain.DataContext = cus;
        }


        private void lblSource_Error(object sender, ValidationErrorEventArgs e)
        {
            lblErrorMessage.Content = e.Error.ErrorContent.ToString();
        }
    }
    }

私の検証規則:

using System.Windows.Controls;

namespace cCompleteWPFResourcesExamples 
{
public class cIDValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, 
System.Globalization.CultureInfo cultureInfo)
    {
        int iValue = (int)value;
        if (iValue == 0) return new ValidationResult(false, "No ID number");

        return new ValidationResult(true, null);
    }
}
}

Customer オブジェクトは非常に単純で、いくつかのプロパティしかありません。

ありがとう!

ジェームズ

4

2 に答える 2

2

Awww 悲しいタイトル :) :) 最初の wpf validationrule は、あなたが望むことをしていません。

バインディング エンジンは、入力値 (バインディング ターゲット プロパティ値) がバインディング ソース プロパティに転送されるたびに、バインディングに関連付けられた各 ValidationRule をチェックします。

これを覚えて:

何かを入力すると、値がソースに保持されます => ValidationRule が起動します。

ラベルに何かを表示したいのですが、値がソースからラベルに送信されています => ValidationRule は起動しません。

例を機能させたい場合は、代わりに TextBox を使用し、Binding Mode を TwoWay に設定して、何かを入力できるようにします。Binding は入力された値をソースに保持し、ValidationRule を起動します。:)

于 2013-11-07T13:24:04.967 に答える
0

私はこのサイトから得た、私のためにそれを機能させるためにこれをしなければなりませんでした

<Binding Path="ID" 
NotifyOnValidationError="True" 
ValidatesOnDataErrors="true" 
ValidatesOnExceptions="True" 
UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">

これが役立つことを願っています。また、object検証に渡されるのはstring、私の場合は ではなかったので注意してくださいint

public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
    if (value is int)
    {
        int iValue = (int)value;
        if (iValue == 0) 
        {
            return new ValidationResult(false, "No ID number");
        }

        return new ValidationResult(true, null);
    }
    else if (value is string)
    { 
        string strValue = (string)value;
        if (String.IsNullOrEmpty(strValue) || strValue == "0")
        { 
            return new ValidationResult(false, "No ID number"); 
        }
    }

    return new ValidationResult(true, null);
}

* アップデート **

トリガーするには、これも追加する必要があることを忘れていました

public wValidationRule()
{
    InitializeComponent();
    cus.ID = 0;
    stkMain.DataContext = cus;

    //trigger the validation.
    lblSource.GetBindingExpression(Label.ContentProperty).UpdateSource();
}
于 2013-11-07T13:51:20.333 に答える