ViewModel의 바운드 속성 설정 도구에서 텍스트의 "기존"상태를 확인합니다. 이 시점에서 이벤트를 발생시키고 발생 상황에 따라 수행 할 작업을 결정하는 메커니즘이 필요합니다.
예 : 적절한 UI를 제시하고 그에 따라 e.Outcome
의 값을 설정 NotExistingNameSet
에 이벤트 핸들러를 추가
enum Outcome { Add, Cancel }
class BlahEventArgs : EventArgs
{
Outcome Outcome { get; set; }
}
class ViewModel
{
private string name;
public EventHandler<BlahEventArgs> NotExistingNameSet;
public Name
{
get { return this.name; }
set
{
if (/* value is existing */) {
this.name = value;
return;
}
var handler = this.NotExistingNameSet;
if (handler == null) {
// you can't just return here, because the UI
// will desync from the data model.
throw new ArgumentOutOfRangeException("value");
}
var e = new BlahEventArgs { Outcome = Outcome.Add };
handler(this, e);
switch (e.Outcome) {
case Outcome.Add:
// Add the new data
this.name = value;
break;
case Outcome.Cancel:
throw new Exception("Cancelled property set");
}
}
}
}
보기.
출처
2011-03-20 11:59:19
Jon