2015-07-20 72 views
0

我试图在Catel的viewModel上实现验证。对viewmodel + Catel进行验证

我阅读文档,它看起来很容易,但不知何故,我可能错过了这个特殊情况。我有一个用户控件,它嵌套了两个用户控件。一个是我将用作自定义命令栏的用户控件,另一个是模型的详细视图。

<catel:UserControl x:Class="SICUAP.Views.CatProducto_CategoriasView" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:catel="http://catel.codeplex.com" 
       xmlns:Views="clr-namespace:SICUAP.Views"> 

<!-- Resources --> 
<UserControl.Resources> 
</UserControl.Resources> 

<!-- Content --> 
<catel:StackGrid> 
    <catel:StackGrid.RowDefinitions> 
     <RowDefinition></RowDefinition> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
    </catel:StackGrid.RowDefinitions> 
    <Views:cmdGlobalesBDView></Views:cmdGlobalesBDView> 
    <Label Content="Catalogo de Categorias de Producto" Style="{StaticResource estiloTituloControl}"> 
    </Label> 
    <Views:dataProducto_CategoriasView /> 

</catel:StackGrid> 

命令栏只有一个具有绑定到全局命令按钮。

<Button Width="50" Height="50" VerticalAlignment="Center" HorizontalAlignment="Center" ToolTip="Salvar" Command="{catel:CommandManagerBinding Salvar}" 
        Style="{StaticResource MetroCircleButtonStyle}"> 
     <Rectangle Width="20" Height="20" Fill="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" > 
      <Rectangle.OpacityMask> 
       <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_save}" /> 
      </Rectangle.OpacityMask> 
     </Rectangle> 
    </Button> 

这是详细视图

<catel:UserControl x:Class="SICUAP.Views.dataProducto_CategoriasView" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:catel="http://catel.codeplex.com"> 
<UserControl.Resources> 
</UserControl.Resources> 
<catel:StackGrid> 
    <catel:StackGrid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
    </catel:StackGrid.RowDefinitions> 
    <GroupBox Header="Datos de la Categoria del Producto"> 
     <catel:StackGrid> 
      <catel:StackGrid.RowDefinitions> 
       <RowDefinition Height="Auto"></RowDefinition> 
       <RowDefinition Height="Auto"></RowDefinition> 
       <RowDefinition Height="Auto"></RowDefinition> 
       <RowDefinition Height="Auto"></RowDefinition> 
      </catel:StackGrid.RowDefinitions> 
      <catel:StackGrid.ColumnDefinitions> 
       <ColumnDefinition Width="300"></ColumnDefinition> 
       <ColumnDefinition Width="100"></ColumnDefinition> 
       <ColumnDefinition Width="200"></ColumnDefinition> 
      </catel:StackGrid.ColumnDefinitions> 
      <Label Content="Nombre:"></Label> 
      <Label Content="Activo:"></Label> 
      <catel:EmptyCell></catel:EmptyCell> 
      <TextBox Margin="5" Text="{Binding Nombre, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"></TextBox> 
      <CheckBox Margin="5" IsChecked="{Binding Activo}"></CheckBox> 
      <catel:EmptyCell></catel:EmptyCell> 
      <Label Content="Descripcion:"></Label> 
      <catel:EmptyRow></catel:EmptyRow> 
      <TextBox Margin="5" TextWrapping="Wrap" AcceptsReturn="True" Grid.ColumnSpan="3" Height="200" Text="{Binding Descr,ValidatesOnDataErrors=True, NotifyOnValidationError=True}"></TextBox> 
     </catel:StackGrid> 
    </GroupBox> 
</catel:StackGrid> 

我创建命令并将其绑定到在细节视图模型的构造验证摘要。

public dataProducto_CategoriasViewModel(ICommandManager commandManager, IMessageService messageService) 
    { 
     Catel.Argument.IsNotNull(() => commandManager); 
     Catel.Argument.IsNotNull(() => messageService); 
     _commandManager = commandManager; 
     _messageService = messageService;    
     Salvar = CommandHelper.CreateCommand(OnSalvarExecute,()=>CategoriaProductoValidationSummary); 
     _commandManager.RegisterCommand(Comandos.Catalogos.Salvar, Salvar, this); 
    } 

这是我对ValidateFields的验证摘要和覆盖的属性。

[ValidationToViewModel(Tag = "ValidacionCategoriaProducto")] 
    public IValidationSummary CategoriaProductoValidationSummary { get; set; } 

    protected override void ValidateFields(List<IFieldValidationResult> validationResults) 
    { 
     if (string.IsNullOrEmpty(Nombre)) 
      validationResults.Add(FieldValidationResult.CreateErrorWithTag(NombrePropiedad, "Nombre es requerido","ValidacionCategoriaProducto")); 
     if (string.IsNullOrEmpty(Descr)) 
      validationResults.Add(FieldValidationResult.CreateErrorWithTag(DescrPropiedad, "Descripcion es requerido", "ValidacionCategoriaProducto"));   
    } 

ValidateFields从不触发。并且该按钮始终处于启用状态。我是否缺少像服务注册或其他东西?

+1

你可以尝试点击确定/保存?可能是验证延迟到第一次保存呼叫。 –

回答

0

已经找到我的错误了。当创建与乐团模板的项目,它在的App.xaml.cs

Catel.Windows.Controls.UserControl.DefaultCreateWarningAndErrorValidatorForViewModelValue = false; 
Catel.Windows.Controls.UserControl.DefaultSkipSearchingForInfoBarMessageControlValue = true; 
Catel.Data.ModelBase.SuspendValidationForAllModels = true; 

他们禁用验证功能,因此删除它们应该是绰绰有余开始添加此行。