当wpf Window Loads文本框得到验证

时间:2015-10-04 08:44:04

标签: c# wpf validation xaml

我是WPF验证的新手。我的问题是,一旦窗口加载,文本框就会得到验证,但我希望文本框在文本框失去焦点时得到验证。

我的XAML文件如下

<Window x:Class="WpfApplicationDtataValidation.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Save"
                    CanExecute="SaveCanExecute"/>
</Window.CommandBindings>
<Grid>
    <TextBlock Text="First Name" Margin="83,70,363,217" RenderTransformOrigin="0.523,0.31"/>
    <TextBlock Text="Last Name" Margin="83,155,363,132" RenderTransformOrigin="0.523,0.31"/>
    <TextBlock Text="Age" Margin="83,231,363,56" RenderTransformOrigin="0.523,0.31"/>

    <TextBox x:Name="tbFirstName" Validation.Error="TbFirstName_OnError"
     Text="{Binding UpdateSourceTrigger=LostFocus, Path=FirstName,
            ValidatesOnDataErrors=true, NotifyOnValidationError=true}" Margin="170,0,0,209" 
             Width="120" Height="45" VerticalAlignment="Bottom" HorizontalAlignment="Left" 
             TabIndex="0" >

    </TextBox>

    <TextBox x:Name="tbLastName" Validation.Error="TbFirstName_OnError"
     Text="{Binding UpdateSourceTrigger=LostFocus, Path=LastName,
            ValidatesOnDataErrors=true, NotifyOnValidationError=true}" Margin="170,0,0,123" 
             Width="120" Height="45" VerticalAlignment="Bottom" HorizontalAlignment="Left" 
             TabIndex="1">

    </TextBox>

    <TextBox x:Name="tbAge" Validation.Error="TbFirstName_OnError"
     Text="{Binding UpdateSourceTrigger=LostFocus, Path=Age,
            ValidatesOnDataErrors=true, NotifyOnValidationError=true}" Margin="170,0,0,49"
             Width="120" Height="45" VerticalAlignment="Bottom" HorizontalAlignment="Left" 
             TabIndex="2">

    </TextBox>
    <Button Command="ApplicationCommands.Save" IsDefault="True" Margin="328,245,128,38" 
            RenderTransformOrigin="0.497,-0.631">Save</Button>
</Grid>

我的Code Behid文件是

namespace WpfApplicationDtataValidation
{
public partial class MainWindow : Window
{
    private int _noOfErrorsOnScreen = 0;
    public MainWindow()
    {
        this.DataContext = new Customer();
        InitializeComponent();
        tbFirstName.Focus();
    }

    private void TbFirstName_OnError(object sender, ValidationErrorEventArgs e)
    {
        if (e.Action == ValidationErrorEventAction.Added)
            _noOfErrorsOnScreen++;
        else
            _noOfErrorsOnScreen--;
    }

    private void SaveCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = _noOfErrorsOnScreen == 0;
    }

 }
}

我的班级文件是

 partial class Customer : IDataErrorInfo  
{
    public string this[string columnName]
    {
        get
        {
            string result = null;
            if (columnName == "FirstName")
            {
                if (string.IsNullOrWhiteSpace(FirstName))
                    result = "Please enter A First Name";
            }
             if (columnName == "LastName")
        {
            if (string.IsNullOrEmpty(LastName))
                result = "Please enter a Last Name";
        }
       if (columnName == "Age")
        {
            if (Age <= 0 || Age >= 99)
                result = "Please enter a valid age";
        }
        return result;
        }
    }

    public string Error { get; private set; }

输出Scree是 enter image description here 我正在使用vs23013 .net 4.5并使用数据库第一实体框架

1 个答案:

答案 0 :(得分:1)

通过使用简单的模式来跟踪第一次是否访问对象,我们可以处理这个问题。

将您的客户类更改为:

partial class Customer : IDataErrorInfo
    {
        static bool isFirstNameInitializing;
        static bool isLastNameInitializing;
        static bool isAgeInitializing;

        static Customer()
        {
            isFirstNameInitializing = true;
            isLastNameInitializing = true;
            isAgeInitializing = true;
        }

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }

        public string this[string columnName]
        {
            get
            {
                string result = null;
                if (columnName == "FirstName")
                {
                    if (!isFirstNameInitializing && string.IsNullOrWhiteSpace(FirstName)) 
                        result = "Please enter A First Name";
                    isFirstNameInitializing = false;
                }
                else if (columnName == "LastName")
                {
                    if (!isLastNameInitializing && string.IsNullOrEmpty(LastName))
                        result = "Please enter a Last Name";
                    isLastNameInitializing = false;
                }
                else if (columnName == "Age")
                {
                    if (!isAgeInitializing && (Age <= 0 || Age >= 99))
                        result = "Please enter a valid age";
                    isAgeInitializing = false;
                }

                return result;
            }
        }

        public string Error { get; private set; }