2014-03-31 2 views
1

프로젝트에서 사용자 입력에 대한 유효성 검사에 IDataErrorInfo를 사용합니다. 또한 MVVM light messaging을 사용하고 있습니다. 그러나 오류 처리가 모든 ShowDialog에 다시 첨부되어 여러 번 발생하는 유효성 검사를 유발하고 대화 상자가 표시 될 때마다 쌓이는 것으로 보입니다. ViewModel은 열기/닫기 중에 유지되며 다시 생성되지 않습니다.IDataErrorInfo가 MVVM Light 메시징과 함께 다시 연결됩니다.

보기 (텍스트 박스)

<TextBox Name="LPNInput" Grid.Column="1" VerticalAlignment="Center" 
     HorizontalAlignment="Stretch" Margin="10,40,10,10" 
     Text="{Binding Path=LPN, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" 
     extensions:FocusExtension.IsFocused="True"> 
    <TextBox.Style> 
    <Style BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type TextBox}"> 
     <Setter Property="Validation.ErrorTemplate" Value="{StaticResource LpnValidationErrorTemplate}" /> 
    </Style> 
</TextBox.Style> 
<TextBox.InputBindings> 
    <KeyBinding Command="{Binding Path=SubmitLPNCommand}" Key="Enter" /> 
</TextBox.InputBindings> 
</TextBox> 

뷰 모델 RequestInputViewModel는

private string lpn; 
public string LPN 
{ 
    get { return lpn; } 
    set 
    { 
     lpn = value; RaisePropertyChanged("LPN"); 
    } 
} 

public string Error { get; private set; } 
public string this[string propertyName] 
{ 
    get 
    { 
     string errorMsg = string.Empty; 
     if (propertyName.Equals("LPN")) 
     { 
      pannenkoek++; 

      if (!string.IsNullOrEmpty(lpn)) 
      { 
       if (!LicensePlateNumber.IsValidLPN(lpn)) 
       { 
        errorMsg = XmlTextProvider.GetHeader("LPNInvalid"); 
       } 
       else if (!someManager.CanAddBag(LicensePlateNumber.Parse(lpn))) 
       { 
        errorMsg = XmlTextProvider.GetHeader("LPNDuplicate"); 
       } 
      } 
     } 

     Error = errorMsg; 
     return Error; 
    } 
} 

private void Close(bool dialogResult) 
{ 
    System.Diagnostics.Debug.WriteLine(string.Format("Pannenkoeken: {0}", pannenkoek)); 

    // Notify the view to close the dialog 
    Messenger.Default.Send<CloseWindowMessage, RequestInputDialog>(new CloseWindowMessage(this, dialogResult)); 
} 

뷰 모델 # 2

RequestLPNViewModel는 뷰 모델 # 2의 생성자에서 생성된다. 디버그 출력

Pannenkoeken에서

Application.Current.Dispatcher.BeginInvoke(new Action(() => Messenger.Default.Send<ShowDialogMessageBase, MainView>(new ShowDialogMessage<RequestInputDialog>(this, RequestInputViewModel, InputEntered)))); 

출력 : 2 Pannenkoeken : 4 Pannenkoeken : 6 Pannenkoeken : 8

나는 숫자의 증가를 기대하지 않을 것이다. 나는 4 배의 숫자 2를 기대했을 것이다. 에러 바인딩은 다이얼로그의 생성시 첨부되지만 다이얼로그가 닫히면 해제되지 않는 것으로 보인다. 그리고 예, int는 열린 상태로 재설정됩니다.

답변

0

대화 상자 동작으로 인해 대화 상자가 생성 될 때마다 viewmodel을 다시 만들도록 선택했습니다. 이전/이전 데이터로 미래의 '이상한'동작을 방지하기 위해 수행하는 것이 더 좋습니다.

viewmodel이 이러한 상태를 나타내지 않습니다.