文字列以外のデータ型にバインドされたTextBoxのSilverlight検証
-
12-12-2019 - |
質問
InotifyDataErrorInfoを使用したSilverlight検証は、文字列以外のデータ型のプロパティにバインドされているテキストボックスで使用しようとしているまで、Slickエラー表示を開始しました。私の計画は、プロパティのセッターを使用して検証ロジックを実行し、必要に応じてそこからエラーを追加して削除することでした。文字列であるテキストボックスでは適していますが、intに束縛されているテキストボックスがある場合は、文字列を入力しても、setterは呼び出されません(ここで、私は明らかに数値以外の値が追加されるエラーを追加することができます。無効)。ここからの提案された行動方針は何ですか?私はValueConvertersを調べましたが、検証されている私のクラスのinotifyDataErrorInfoロジックから離れています。
仮説的な例:
public class MyClass
{
private string _prod;
public string Prod
{
get { return _prod; }
set //This works great
{
if (value.Length > 15)
{
AddError("Prod", "Cannot exceed 15 characters", false);
}
else if (value != _prod)
{
RemoveError("Prod", "Cannot exceed 15 characters");
_prod= value;
}
}
}
private int _quantity;
public int Quantity
{
get { return _quantity; }
set //if a string was entered into the textbox, this setter is not called.
{
int test;
if (!int.TryParse(value, test))
{
AddError("Quantity", "Must be numeric", false);
}
else if (test != _quantity)
{
RemoveError("Quantity", "Must be numeric");
_quantity= test;
}
}
}
protected Dictionary<String, List<String>> errors =
new Dictionary<string, List<string>>();
public void AddError(string propertyName, string error, bool isWarning)
{
//adds error to errors
}
public void RemoveError(string propertyName, string error)
{
//removes error from errors
}
//INotifyDataErrorInfo members...
}
. 解決
私は、とにかくTextBoxの値を文字列値にバインドし、そこで検証を行います。 検証が成功した場合は、実際に検索しているデータ型を使用して別のプロパティに値を渡します(例えば、int)。その他の場合では、検証に失敗します。 ただの回避策...しかし私のために働いています。
所属していません StackOverflow