私は WPF で検証を実装しようと取り組んでおり、検証が失敗した後にテキストボックスの値をクリックまたは変更できないという問題が発生しています。
次の関連コードを含む User クラス (IDataErrorInfo を実装) があります。
public virtual string Error
{
get
{
return null;
}
}
public virtual string this[string name]
{
get
{
string result = null;
if (name == "uFirstName")
{
if (String.IsNullOrEmpty(this.uFirstName))
{
return "Must enter a first name!";
}
}
return result;
}
}
次に、MainWindow のコード ビハインドに、コンボボックスを接続する次のコードがあります。
comboBox1.ItemsSource = Users; //Users is a collection of Users
最後に、MainWindow xaml には次のようなものがあります。
<ComboBox Name="comboBox1" ItemTemplate="{StaticResource userTemplate}" />
<TextBox Name="textBox1" DataContext="{Binding ElementName=comboBox1,
Path=SelectedItem}" Text="{Binding Path=uFirstName, ValidatesOnDataErrors=True,
NotifyOnValidationError=True}"/>
実際、テキストを削除すると検証が起動し、テキストボックスに赤い境界線が表示されます。ただし、変更は引き続きユーザーに送信されます (uFirstName は何も設定されません!)。さらに悪いことに、タブで戻らない限り、そのテキストボックスの値を編集できなくなりました。
値が有効でない場合に値が返されないようにし、無効な場合にテキストボックスを編集できるようにするには何が必要ですか?