2010-11-26 4 views
0

내 MVVM 구현을 돕기 위해 JulMar Mvvm-Helpers을 사용하는 새 C# 4.0/Prism 4 응용 프로그램을 개발 중입니다. 내 검증 로직에 문제가 있습니다. 과거에는 Prism 2.2/Enterprise Library Validation Block을 성공적으로 사용했습니다. 그러나이 프로젝트를 위해 저는 새로운 것을 시도하고 있습니다.MVVM-Helpers 유효성 검사 속성을 사용하는 WPF MVVM 유효성 검사 문제

내 XAML 코드는 아래와 같습니다.

<UserControl x:Class="DoSomeThing.Views.DoSomeThingView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:Converters="clr-namespace:JulMar.Windows.Converters;assembly=JulMar.Wpf.Helpers" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300" > 
<UserControl.Resources> 
    <Converters:ValidationErrorConverter x:Key="errorConverter"/> 
    <Style TargetType="{x:Type TextBox}"> 
     <Style.Triggers> 
      <Trigger Property="Validation.HasError" Value="true"> 
       <Setter Property="ToolTip" 
        Value="{Binding RelativeSource={RelativeSource Self}, 
        Path=(Validation.Errors), Converter={StaticResource errorConverter}}"/> 
       <Setter Property="Background" Value="Red" /> 
       <Setter Property="Foreground" Value="White" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</UserControl.Resources> 
<Grid Name="EditGrid" Margin="3"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="10" /> 
     <ColumnDefinition Width="100" /> 
     <ColumnDefinition Width="*" /> 
    </Grid.ColumnDefinitions> 

    <Label Grid.Column="1" Grid.Row="0" Content="Name" Height="21" VerticalAlignment="Top" /> 
    <Label Grid.Column="1" Grid.Row="1" Content="Address" /> 
    <Label Grid.Column="1" Grid.Row="2" Content="Zip" /> 
    <Label Grid.Column="1" Grid.Row="3" Content="Number Of Doors" /> 
    <Label Grid.Column="1" Grid.Row="4" Content="Double Number" /> 


    <TextBox Grid.Column="2" Grid.Row="0" Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" 
      HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" /> 
    <TextBox Grid.Column="2" Grid.Row="1" Text="{Binding Path=Address, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" 
      HorizontalAlignment="Left" VerticalAlignment="Top" Width="200"/> 
    <TextBox Grid.Column="2" Grid.Row="2" Text="{Binding Path=Zip, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" HorizontalAlignment="Left" 
      VerticalAlignment="Top" Width="200"/> 
    <TextBox Grid.Column="2" Grid.Row="3" Text="{Binding Path=NumberOfDoors, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" 
      HorizontalAlignment="Left" Height="21" Width="200"/> 
    <TextBox Grid.Column="2" Grid.Row="4" Text="{Binding Path=DoubleNumber, Mode=TwoWay, 
     UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, 
     ValidatesOnExceptions=True}" 
      HorizontalAlignment="Left" VerticalAlignment="Top" Width="200"/> 
    <Button Content="Save" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="4" Height="23" 
      HorizontalAlignment="Left" Margin="26,41,0,0" Name="button1" VerticalAlignment="Top" 
      Width="75" Command="{Binding SaveCommand}"/> 
</Grid> 

뷰 모델에보기를 결합하는 코드는 유효성 검증 뷰 모델에 속성처럼

  IRegion region = this._regionManager.Regions["MainRegion"]; 
     var v = new DoSomeThingView(); 
     var model = new SampleDataModel 
      { 
       Name = "hello", 
       NumberOfDoors = 5, 
       Zip = "12345", 
       DoubleNumber = 321.12, 
       Address = "no where's ville" 
      }; 

     var vm = new SampleDataViewModel { DataModel = model }; 

     v.EditGrid.DataContext = vm; 
     region.Add(v); 

모든 편집 논리가 작동합니다. 내 문제는 bool CanSaveCommand(object param) 함수를 유효성 검사 오류가 있으면 저장을 방지하는 것입니다.

유효성 검사 오류가 있음을 감지하는 방법을 알지 못합니다. 제안?

답변

0

내 문제를 해결하기 위해 viewmodel 유효성 검사 루틴을 명시 적으로 호출하고 결과를 확인했습니다.

내 viewmodel은 ValidatingViewModel 기본 클래스에서 상속되었습니다. 첫 번째 매개 변수에 널 (null)로

private bool CanSaveExecute(object param) 
{ 
    string v = ValidationManager.Validate(null, this); 
    bool b = v.Length == 0; 
    return b; 
} 

그것을 검증 속성을 가지고있는 뷰 모델의 모든 특성을 테스트, - 다음과 같이 즉, 정적 ValidationManager 클래스에 액세스 할 수 있습니다. 그런 다음 반환 된 오류 메시지 문자열의 길이가> 0이면 유효성 검사 오류가 있음을 감지 할 수 있습니다.