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

WPF-如何验证对象的新实例?,wpf,validation,instance,datacontext,Wpf,Validation,Instance,Datacontext,我有这门课 public class Doctor { private string licenseNumber; public string LicenseNumber { get { return this.licenseNumber; } set { if (value.Length > 4) thro

我有这门课

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形式,并将值输入到props。 现在,我创建了一个表单,用户可以在其中添加一个新的医生。 问题是-我想在用户单击“添加”之前验证表单。 这是我的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属性附加到任何东西,因为我没有医生实例,当然我也不能创建一个空实例,因为我自己创建的所有异常。。。 我很乐意知道向数据库添加数据的逻辑,并在之前验证它,而不仅仅是在更新中。
你看了吗?这将使您的代码更干净,您不必再抛出异常,因为用户将无法向数据库添加无效数据。

基本上您是对的,这是一种更干净的方法。但例外情况的存在是有原因的。这个类还有一些其他的植入,不是全部通过UI。因此,例外情况是“基本”验证器,它确保无效数据进入类。问题是我是否可以在表单中使用它。我认为如果你在setter绑定到UI时抛出一个异常,那就太过分了?是否有任何类/对象可以设置那些不来自UI的属性?