首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >视图模型+ Catel的验证

视图模型+ Catel的验证
EN

Stack Overflow用户
提问于 2015-07-20 19:19:56
回答 1查看 524关注 0票数 1

我试图在Catel中实现viewModel的验证。

我读了这些文档,看起来很简单,但不知怎么的,我可能遗漏了这个特殊情况下的一些东西。我有一个用户控件,其中嵌套了两个用户控件。一个是用户控件,我将使用它作为自定义命令栏,另一个是模型的详细视图。

代码语言:javascript
运行
复制
<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>

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

代码语言:javascript
运行
复制
<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>

这是细节图

代码语言:javascript
运行
复制
<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>

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

代码语言:javascript
运行
复制
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的覆盖。

代码语言:javascript
运行
复制
[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从不触发。这个按钮总是被启用的。我是不是漏掉了服务登记簿之类的东西?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-21 16:31:28

已经发现了我的错误。当使用管弦乐队模板创建项目时,它在App.xaml.cs的开头添加以下行

代码语言:javascript
运行
复制
Catel.Windows.Controls.UserControl.DefaultCreateWarningAndErrorValidatorForViewModelValue = false;
Catel.Windows.Controls.UserControl.DefaultSkipSearchingForInfoBarMessageControlValue = true;
Catel.Data.ModelBase.SuspendValidationForAllModels = true;

它们禁用验证功能,因此删除它们就足够了。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31524642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档