2010-11-26 16 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> 

のviewmodelにビューをバインドするコードは、ビューモデルの検証属性がそうであるように

  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検証ルーチンを明示的に呼び出して結果を確認しました。

私のビューモデルはValidatingViewModelベースクラスから継承されています。次のようにそれは、静的ValidationManagerクラスへのアクセスを可能にする -

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

最初のパラメータはnullで、それは検証属性を持っているのviewmodel上のすべてのプロパティをテストします。次に、返されたエラーメッセージ文字列の長さ> 0の場合、検証エラーが存在することを検出できます。