在WPF 中用DatePicker控件输入出生年月,按回车键自动计算年龄,代码如下
UI界面
<Window x:Class="WPF通过出生日期计算年龄.MainWindow"
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:local="clr-namespace:WPF通过出生日期计算年龄"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DatePicker Name="dteBirthday" HorizontalAlignment="Left" Margin="163,102,0,0" VerticalAlignment="Top" Height="26" Width="166" PreviewKeyDown="DteBirthday_PreviewKeyDown"/>
<TextBox Name="tbAge" HorizontalAlignment="Left" Height="23" Margin="163,158,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="76"/>
<ComboBox Name="cbAge" HorizontalAlignment="Left" Margin="260,158,0,0" VerticalAlignment="Top" Width="69">
<ComboBoxItem Content="岁"/>
<ComboBoxItem Content="月"/>
<ComboBoxItem Content="日"/>
</ComboBox>
<Label Content="出生日期:" HorizontalAlignment="Left" Margin="81,101,0,0" VerticalAlignment="Top"/>
<Label Content="年 龄:" HorizontalAlignment="Left" Margin="81,156,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
后台代码:
注意,DatePicker键盘事件要用PreviewKeyDown而不用KeyDown事件
using System;
using System.Windows;
using System.Windows.Input;
namespace WPF通过出生日期计算年龄
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void DteBirthday_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
try
{
//dteBirthday为空时跳出
DateTime dateTime = (DateTime)dteBirthday.SelectedDate.Value;
}
catch (Exception)
{
return;
}
DateTime now = DateTime.Now;//当前时间用于对比
if (dteBirthday.SelectedDate > DateTime.Now)
{
MessageBox.Show("出生日期不能大于当前时间");
return;
}
int age = now.Year - dteBirthday.SelectedDate.Value.Year;//计算年龄
if (age > 1)
{
if ((now.Month > dteBirthday.SelectedDate.Value.Month) || (now.Month == dteBirthday.SelectedDate.Value.Month && now.Day >= dteBirthday.SelectedDate.Value.Day))
{
tbAge.Text = age.ToString();
cbAge.SelectedIndex = 0;
}
else
{
age--;
tbAge.Text = age.ToString();
cbAge.SelectedIndex = 0;
}
}
else if (age == 1)
{
if ((now.Month > dteBirthday.SelectedDate.Value.Month) || (now.Month == dteBirthday.SelectedDate.Value.Month && now.Day >= dteBirthday.SelectedDate.Value.Day))
{
tbAge.Text = "1";
cbAge.SelectedIndex = 0;
}
else if (now.Month == dteBirthday.SelectedDate.Value.Month && now.Day < dteBirthday.SelectedDate.Value.Day)
{
tbAge.Text = "11";
cbAge.SelectedIndex = 1;
}
else
{
age = now.Month + 12 - dteBirthday.SelectedDate.Value.Month;
tbAge.Text = age.ToString();
cbAge.SelectedIndex = 1;
}
}
else
{
if (now.Month > dteBirthday.SelectedDate.Value.Month)
{
age = now.Month - dteBirthday.SelectedDate.Value.Month;
tbAge.Text = age.ToString();
cbAge.SelectedIndex = 1;
}
else if ((now.Month == dteBirthday.SelectedDate.Value.Month) && (now.Day > dteBirthday.SelectedDate.Value.Day))
{
age = now.Day - dteBirthday.SelectedDate.Value.Day;
tbAge.Text = age.ToString();
cbAge.SelectedIndex = 2;
}
else
{
tbAge.Text = "1";
cbAge.SelectedIndex = 2;
}
}
}
}
}
}
效果图
思路其实很简单,先画一个树状图理清思路
在将共用部分合并
项目完整源码地址
https://gitee.com/xiaoniushengdaniu/wpf_birth_date_calculates_age