0

私はクラス Client を持っています:

public class Client : INotifyPropertyChanged
{

    private string m_strCode;
    public string strCode
    {
        get { return m_strCode; }
        set
        {

            if (CodeIsValide(strCode))
            {                  
                m_strCode = value;
                FirePropertyChangedEvent("strCode");

            }
            else
            {
                throw new ApplicationException("bad Data !");
            }

            FirePropertyChangedEvent("strCode");

        }
    }

    private string m_strName;
    public string strName
    {
        get { return m_strName; }
        set
        {

            if (NameIsValide(strName))
            {
                m_strName = value;
                FirePropertyChangedEvent("strName");

            }
            else
            {
                throw new ApplicationException("Bad Data! ");
            }

            FirePropertyChangedEvent("strName");

        }
    }

    public Client(string Code, string Name)
    {
        strCode = Code;
        strName = Name;
    }

    public Client()
    {

    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void FirePropertyChangedEvent(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

私の WPF ウィンドウには、client.strCode 用の on と client.strNom 用の 2 つのテキスト ボックスがあります。

XAML は次のとおりです。

<TextBox x:Name="TextBox_Code"
         HorizontalAlignment="Left"
         Height="25"
         Margin="106,230,0,0"
         TextWrapping="Wrap"
         VerticalAlignment="Top"
         Width="78">
  <TextBox.Text>
    <Binding Path="strCode"
             UpdateSourceTrigger="PropertyChanged">
      <Binding.ValidationRules>
        <ExceptionValidationRule />
      </Binding.ValidationRules>
    </Binding>
  </TextBox.Text>
</TextBox>

<TextBox x:Name="TextBox_Name"
         HorizontalAlignment="Left"
         Height="25"
         Margin="106,230,0,0"
         TextWrapping="Wrap"
         VerticalAlignment="Top"
         Width="78">
  <TextBox.Text>
    <Binding Path="strName"
             UpdateSourceTrigger="PropertyChanged">
      <Binding.ValidationRules>
        <ExceptionValidationRule />
      </Binding.ValidationRules>
    </Binding>
  </TextBox.Text>
</TextBox>

データ入力は ValidationProcess で検証されています。検証が false の場合、テキスト ボックスの近くに警告ラベルが表示されます。

私の問題は次のとおりです:クライアントオブジェクトのコレクションに(データベースから)悪いデータをロードした場合。読み込みは strCode と strName の set() を使用し、データが悪い場合は ExceptionValidationRule をスローできません (バインドされたテキスト ボックスから ExceptionValidationRule が呼び出されないためだと思います)。(私は英語が苦手です。申し訳ありませんが、説明するのは簡単ではありません)。

検証プロセスをいつ呼び出すかを指定する必要があると思います。

誰でもアドバイスをお願いしますか?

必要に応じて、問題を共有するためにサンプル VS プロジェクトを作成します。

EDIT:カスタムバリデータークラスを使用すればOKです!

質問ですが、検証テストを強制するには、 DataGrid で行を選択するときにこれを行う必要があります。

    private void myDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        // Affiche le code évt sélectionné dans le tableau, dans les champs modifiable ( en haut de l'écran )

        var item = myDataGrid.SelectedItem as Client;
        if ((item != null))
        {
            TextBox_Code.Text = item.strCode;
            TextBox_Name.Text = item.strName;
        }
    }

:

TextBox_Code.Text = item.strCode;
TextBox_Name.Text = item.strName;

この2行を削除すると、バインディングのためにテキストボックスが正しく初期化されますが、検証プロセスは呼び出されません。なんで ?検証プロセスを強制し、完全なバインディング whitout を使用する方法はありますか:

TextBox_Code.Text = item.strCode;
TextBox_Name.Text = item.strName;

ありがとう :)

どうもありがとう :)

よろしくお願いします、

ニクセウス:)

4

1 に答える 1

1

あなたが何を必要としているのかを正しく理解できれば、

プロパティが xaml ビュー以外から設定されている場合、例外を発生させたくありません。

私の好ましい解決策は、まったく使用せずExceptionValidationRule、 Custom を好むことValidationRuleです。これで問題が解決します。は、そのような機能を作成して使用する方法を示しています。

カスタム検証ルールの例:

public class CodeRangeRule : ValidationRule {
  public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
    int strCode = -1;
    if (value != null && !Int32.TryParse(value.ToString(), out strCode))
      return new ValidationResult(false, "Illegal strCode");
    if (strCode < 9999 && strCode > 0) // Modify to suit your Validity Conditions
      return new ValidationResult(true, null);
    return new ValidationResult(false, "strCode is not within acceptable code range");
  }
}

そしてあなたのxaml:

<TextBox x:Name="TextBox_Code" HorizontalAlignment="Left" Height="25" Margin="106,230,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="78">
    <TextBox.Text>
        <Binding Path="strCode"  UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <local:CodeRangeRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

また

ValidationRule何らかの理由でカスタムを使用したくない場合ExceptionValidationRuleは、ビューにそのまま保持し、データベースのロードからセッターを呼び出す前に try キャッチを追加するか、

public class Client : INotifyPropertyChanged

オブジェクトがビュー/データベースによって処理されているかどうかを示すために、別のプロパティ(値を持つ列挙型など)を追加しますkDatabaseLoadkViewしたがって、あなたのセッターで、この列挙型が例外をスローする前にあるかどうかstrCodeを確認してください。strNamekView

データベースのロードを実行してオブジェクトを作成すると、設定する前にClient列挙型が割り当てられ、. ロードが完了したら、必ず列挙型を元に戻してください。kDatabaseLoadstrNamestrCodekView

于 2013-04-18T09:45:28.930 に答える