65.9K
CodeProject 正在变化。 阅读更多。
Home

带数据窗体的自定义 ValidationSummary -Silverlight 4

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.09/5 (5投票s)

2010年9月13日

CPOL
viewsIcon

45834

downloadIcon

1198

通过隐藏数据窗体的默认验证摘要来自定义验证摘要

引言

感谢所有成员。

我从 CodeProject 社区找到了许多解决问题的方案,现在轮到我贡献了!

在本文中,我将解释如何通过隐藏数据窗体的默认验证摘要来实现自定义验证摘要。

背景

以下是我关注的解决方案

  1. 使用面向对象设计原则的 MVVM 实现
  2. 领域层设计提示
  3. 自定义验证摘要
  4. 验证错误的一致性

Using the Code

用户界面

示例包含三个字段,用于显示姓名、地址和年龄。以下是我的 UI,它是一个数据窗体。一个非常简单的 UI。

Fig-1.gif

视图模型实现

以下是我的视图模型实现的类图。

Fig-2.gif

以下是 MyViewModel 类的源代码。

MyViewModel.cs

using System;
using System.ComponentModel.DataAnnotations;

namespace CustomValidationWithDataForm
{
    public class MyViewModel : ViewModelBase
    {
        public MyViewModel()
            : base()
        {
            if (null == Data)
                this.Data = Activator.CreateInstance(typeof(Person)) as Person;
        }
        [Required(ErrorMessage="Name should not be empty!")]
        public string Name
        {
            get
            {
                return this.Data.Name;
            }
            set
            {
                ValidateProperty(value, "Name");
                this.Data.Name = value;
            }
        }

        [Required(ErrorMessage = "Address should not be empty!")]
        public string Address
        {
            get
            {
                return this.Data.Address;
            }
            set
            {
                ValidateProperty(value, "Address");
                this.Data.Address = value;
            }
        }
        [Range(20,50,ErrorMessage="Age should be between 20 and 50!")]
        [CustomValidation(typeof(AgeValidater),"ValidateAge")]
        public int Age
        {
            get
            {
                return this.Data.Age;
            }
            set
            {
                ValidateProperty(value, "Age");
                this.Data.Age = value;
            }
        }
    }
}

验证

以下是我在此示例中遵循的验证规则

  • 姓名和地址不能为空。
  • 年龄应在 20 到 50 之间,且不等于 35(自定义验证)。
  • 您可以在上面的代码片段中看到验证属性。

关注点

自定义验证摘要

我使用 ValidationSummaryStyle 折叠了数据窗体特定的 ValidationSummary

请参阅下面的 MainPage.xaml

<UserControl x:Class="CustomValidationWithDataForm.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="clr-namespace:System.Windows.Controls;
assembly=System.Windows.Controls.Data.DataForm.Toolkit"
xmlns:input="clr-namespace:System.Windows.Controls;
assembly=System.Windows.Controls.Data.Input"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<Style x:Key="CustomValidationSummaryStyle" TargetType="input:ValidationSummary">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</Grid.Resources>
<input:ValidationSummary x:Name="CustomValidationSummary"/>
<toolkit:DataForm x:Name="myDf" Width="400" Height="400" 
HorizontalAlignment="Center" 
Header="My Details" AutoCommit="False" AutoEdit="True" 
ValidationSummaryStyle="{StaticResource CustomValidationSummaryStyle}" >

<StackPanel>
<toolkit:DataField Label="Name:">
<TextBox x:Name="txtName" Text="{Binding Name,Mode=TwoWay}"/>
</toolkit:DataField>
<toolkit:DataField Label="Address:">
<TextBox x:Name="txtAddress" Text="{Binding Address,Mode=TwoWay}"/>
</toolkit:DataField>
<toolkit:DataField Label="Age:">
<TextBox x:Name="txtAge" Text="{Binding Age,Mode=TwoWay}"/>
</toolkit:DataField> 
</StackPanel>
</toolkit:DataForm>
</Grid>
</UserControl>

历史

  • 2010年9月13日:初始发布
© . All rights reserved.