WPF - 如何验证对象的新实例?

时间:2013-12-09 19:57:01

标签: wpf validation instance datacontext

我有这个班级

public class Doctor
{
    private string licenseNumber;
    public string LicenseNumber
    {
        get
        {
            return this.licenseNumber;
        }
        set
        {
            if (value.Length > 4)
                throw new MyException("License Number can't be more then 4 digits.\n");
            if (!new Regex("^[0-9]*$").Match(value).Success) // Allow only numbers
                throw new MyException("Only numbers are allowed in a License Number.\n");
            this.licenseNumber = value.PadLeft(4,'0'); // Pad with zeros to 4 digits
        }
    }
    private string name;
    public string Name
    {
        get
        {
            return this.name;
        }
        set
        {
            if (!new Regex("^[a-zA-Z ]*$").Match(value).Success) // Allow only letters and spaces 
                throw new MyException("Only letters and spaces are allowed in a name.\n");
            this.name = value;
        }
    }
    private string phoneNumber;
    public string PhoneNumber
    {
        get
        {
            return this.phoneNumber;
        }
        set
        {
            {
                if (!new Regex("^0[2-9]-[0-9]{7}$").Match(value).Success) // allow only numbers in format 0[2-9]-xxxxxxx
                    throw new MyException("ERROR: only numbers in format 0[2-9]-xxxxxxx are legal\n");
                this.phoneNumber = value;
            }
        }
    }

}

内部有更多属性。 当然,这个类的ctor只接受里面有值的ctor形式,并输入道具的值。 现在,我已经创建了一个表单,在用户内部可以添加一个新的Doctor。 事情是 - 我想在用户点击“添加”之前验证表单。 这是我在XML中的“添加”表单的缩写版本:

<Window x:Class="PLForms.AddDoctor"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AddDoctor" Height="431" Width="381" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:BE;assembly=BE" Loaded="Window_Loaded" Background="White" ResizeMode="CanResizeWithGrip" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Margin="12" HorizontalAlignment="Left" VerticalAlignment="Top">
<Window.Resources>
    <CollectionViewSource x:Key="doctorViewSource" d:DesignSource="{d:DesignInstance my:Doctor, CreateList=True}" />
</Window.Resources>
    <Grid Height="367" Name="grid1" Width="296" Margin="12" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="29" />
        </Grid.RowDefinitions>
        <Grid DataContext="{StaticResource doctorViewSource}" HorizontalAlignment="Left" Margin="19,13,0,0" Name="grid2" VerticalAlignment="Top">
            <Label Content="Name:" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
            <TextBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="3" Name="nameTextBox" Text="{Binding Path=Name, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
            <Label Content="Phone Number:" Grid.Column="0" Grid.Row="3" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
            <TextBox Grid.Column="1" Grid.Row="3" Height="23" HorizontalAlignment="Left" Margin="3" Name="phoneNumberTextBox" Text="{Binding Path=PhoneNumber, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
        </Grid>
        <Button Content="Add" Name="Add" BorderBrush="#FF612355" Foreground="White" Click="Add_Click" Margin="0,326,0,0" Grid.RowSpan="2">
            <Button.Background>
                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                    <GradientStop Color="Black" Offset="0" />
                    <GradientStop Color="#85081DDA" Offset="0.893" />
                </LinearGradientBrush>
            </Button.Background>
        </Button>
    </Grid>

现在,我认为只有在我以某种方式设置“DataContext”时才会激活数据验证。我试图在表单中设置一个示例Doctor对象,将DataContext分配给该对象的实例,然后 - 当用户将数据更新为非法数据时 - 它激活验证,并在文本框周围标记红色边框。 但我正在寻找如何“激活”验证,即使之前没有医生的实例 - 当用户获得一个空白表格时,自己填写,然后尝试点击“添加” 。 我的问题是,通过这种方式,我无法将DataContext属性附加到任何东西,因为我没有Doctor实例,当然我也无法创建一个空实例,因为我自己创建了所有异常。 。 我很乐意知道添加数据到数据库的逻辑是什么,并在之前验证它,而不仅仅是在更新中。 Ť

1 个答案:

答案 0 :(得分:0)

你看过validation attributes了吗?这应该使您的代码更清晰,并且您不必再抛出异常,因为用户将无法向数据库添加无效数据。