简体   繁体   English

如何在C#中多次为DatePicker设置正确的验证?

[英]How to set the correct validation for DatePicker more than once in C#?

I'm having validation problems with the Date of Birth. 我的出生日期有验证问题。 I debugged it by not filling in all fields and then I noticed 'dateOfBirthPicker_ValueChanged' will still be invoked due to the this.dateOfBirthPicker = dtN, occurring programmatically. 我通过不填写所有字段来调试它,然后我注意到由于this.dateOfBirthPicker = dtN(以编程方式发生),“ dateOfBirthPicker_ValueChanged”仍将被调用。 When I do actually use the 'dateofBirthPicker', the 'dateOfBirthTBL' Textblock is still red, bold, and cotain an asterisk. 当我确实使用'dateofBirthPicker'时,'dateOfBirthTBL'文本块仍然是红色,粗体且包含星号。 As, in the if-statement, I want to remove it back to default after the user has clicked on, opened and modified the datePicker, but I'm not sure how to do this logically. 因为,在if语句中,我想在用户单击,打开和修改datePicker之后将其恢复为默认值,但是我不确定如何逻辑地执行此操作。

Also how can I restrict the max date for the datePicker because every time I try to use one of those date methods, it gives me a missing directive or assembly reference. 另外,如何限制datePicker的最大日期,因为每次尝试使用这些date方法之一时,都会给我丢失的指令或程序集引用。 I don't know which class or package to be able to use it. 我不知道可以使用哪个类或包。

dateOfBirthPicker.DisplayDateEnd = DateTime.Now;

C# Code: C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Media; // added to support SolidColorBrush, FontWeights, etc...
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Data_Query.Resources;
using System.Windows.Input;

namespace Data_Query
{
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    DateTime dtN; // var to set current time
    bool clicked = false; // returns true if the 'fingerprintB' Button or DateTimePicker was clicked at least once; otherwise false.

    // user's date of birth by using a date picker
    private void dateOfBirthPicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
    {
        if(clicked) return;
        //MessageBox.Show("You are in the DateTimePicker.ValueChanged event.");
    }

    // validates first, beforing sending the user's information into a database query 
    private void submitButton(object sender, RoutedEventArgs e)
    {
        //------------------------------------FIRST NAME-------------------------------//
        if(string.IsNullOrWhiteSpace(firstNameTB.Text) || firstNameTB.Text == "")
        {
            firstNameTBL.Text = "First Name: *";
            firstNameTBL.Foreground = new SolidColorBrush(Colors.Red);
            firstNameTBL.FontWeight = FontWeights.Bold;
        }
        else if(!(string.IsNullOrWhiteSpace(firstNameTB.Text) || firstNameTB.Text == ""))
        {
            // set back to default layout
            this.firstNameTBL.ClearValue(TextBlock.ForegroundProperty);
            this.firstNameTBL.ClearValue(TextBlock.FontWeightProperty);
            this.firstNameTBL.Text = "First Name:";
        }
        //------------------------------------LAST NAME-------------------------------//
        if(string.IsNullOrWhiteSpace(lastNameTB.Text) || lastNameTB.Text == "")
        {
            lastNameTBL.Text = "Last Name: *";
            lastNameTBL.Foreground = new SolidColorBrush(Colors.Red);
            lastNameTBL.FontWeight = FontWeights.Bold;
        }
        else if(!(string.IsNullOrWhiteSpace(lastNameTB.Text) || lastNameTB.Text == ""))
        {
            // set back to default layout
            this.lastNameTBL.ClearValue(TextBlock.ForegroundProperty);
            this.lastNameTBL.ClearValue(TextBlock.FontWeightProperty);
            this.lastNameTBL.Text = "Last Name:";
        }
        //------------------------------------EMAIL ADDRESS-------------------------------//
        if(string.IsNullOrWhiteSpace(emailAddressTB.Text) || emailAddressTB.Text == "")
        {
            emailAddressTBL.Text = "Email Address: *";
            emailAddressTBL.Foreground = new SolidColorBrush(Colors.Red);
            emailAddressTBL.FontWeight = FontWeights.Bold;
        }
        else if (!(string.IsNullOrWhiteSpace(emailAddressTB.Text) || emailAddressTB.Text == ""))
        {
            // set back to default layout
            this.emailAddressTBL.ClearValue(TextBlock.ForegroundProperty);
            this.emailAddressTBL.ClearValue(TextBlock.FontWeightProperty);
            this.emailAddressTBL.Text = "Email Address:";
        }
        //------------------------------------DATE OF BIRTH-------------------------------//
        //if()
        //dateOfBirthPicker.DisplayDateEnd = DateTime.Now;

        clicked = true; // prevent the validation code from running
        dtN = DateTime.Now; // initialize DateTime.Now to be use in value comparsion with DateTimePicker
        this.dateOfBirthPicker.Value = dtN; // set current time for DateTimePicker to match dtN
        clicked = false; // allow it to run again
        if(dateOfBirthPicker.Value == dtN) // true when user hasn't changed or set the DateTimePicker
        {
            dateOfBirthTBL.Text = "Date of Birth: *";
            dateOfBirthTBL.Foreground = new SolidColorBrush(Colors.Red);
            dateOfBirthTBL.FontWeight = FontWeights.Bold;
        }
        else if(clicked)
        {
            // set back to default layout
            this.dateOfBirthTBL.ClearValue(TextBlock.ForegroundProperty);
            this.dateOfBirthTBL.ClearValue(TextBlock.FontWeightProperty);
            this.dateOfBirthTBL.Text = "Date of Birth:";
        }
        //------------------------------------GENDER-------------------------------//
        if(maleRB.IsChecked == false && femaleRB.IsChecked == false)
        {
            genderTBL.Text = "Gender: *";
            genderTBL.Foreground = new SolidColorBrush(Colors.Red);
            genderTBL.FontWeight = FontWeights.Bold;
        }
        else if(!(maleRB.IsChecked == false && femaleRB.IsChecked == false))
        {
            // set back to default layout
            this.genderTBL.ClearValue(TextBlock.ForegroundProperty);
            this.genderTBL.ClearValue(TextBlock.FontWeightProperty);
            this.genderTBL.Text = "Gender:";
        }
        //------------------------------------FINGERPRINT-------------------------------//
        if(!clicked)
        {
            MessageBox.Show("Fingerprint Scan was not completed! \nPlease scan your fingerprint until the progress bar is full!", "Notice!", MessageBoxButton.OK);
            fingerprintTBL.Text = "Fingerprint: *";
            fingerprintTBL.Foreground = new SolidColorBrush(Colors.Red);
            fingerprintTBL.FontWeight = FontWeights.Bold;
        }
        else if(clicked)
        {
            // set back to default layout
            this.fingerprintTBL.ClearValue(TextBlock.ForegroundProperty);
            this.fingerprintTBL.ClearValue(TextBlock.FontWeightProperty);
            this.fingerprintTBL.Text = "Fingerprint:";
        }

    }

    // clear all user's information inputs
    private void resetButton(object sender, RoutedEventArgs e)
    {
        firstNameTB.Text = string.Empty;
        lastNameTB.Text = string.Empty;
        emailAddressTB.Text = string.Empty;
        dateOfBirthPicker.Value = DateTime.Now;
        maleRB.IsChecked = false;
        femaleRB.IsChecked = false;
        clicked = false;
    }

    // supply a method to allow you to use the SIP Enter key to dismiss the SIP
    private void TextBox_KeyUp(object sender, KeyEventArgs e)
    {
        if(e.Key == Key.Enter)
        {
            this.Focus();
        }
    }
}

} }

You could use a Boolean flag. 您可以使用布尔标志。 For example: 例如:

var manualDateChange = false;

void datepicker_valuechange(object sender, DateTimeValueChangedEventArgs e)
{
    if(manualDateChange) return;

    //Do your validation here
}

void someFunctionThatChangesTheDate()
{
    manualDateChange = true; //prevent the validation code from running
    datepicker.Value = DateTime.Now;
    manualDateChange = false; //allow it to run again
}

Note: if you are concerned that changing manualDateChange will allow the user to change it without validation, remember that all event handling code is executed on the main thread and that while the code is running, the user cannot interact with any user interface elements. 注意:如果您担心更改manualDateChange将使用户无需验证即可对其进行更改,请记住所有事件处理代码都在主线程上执行,并且在代码运行时,用户无法与任何用户界面元素进行交互。 So as long as you set the flag, set the value, and reset the flag, its safe. 因此,只要您设置标志,设置值并重置标志,它就安全了。

EDIT: If all you want to do is set the max date, you want to set DatePicker.MaxYear since that's the only real option you have in Windows Phone 8. http://msdn.microsoft.com/en-US/library/windows/apps/windows.ui.xaml.controls.datepicker.maxyear 编辑:如果要设置最大日期, DatePicker.MaxYear设置DatePicker.MaxYear因为这是Windows Phone 8中唯一的真实选项DatePicker.MaxYear : //msdn.microsoft.com/zh-CN/library/窗口/应用/ windows.ui.xaml.controls.datepicker.maxyear

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM