您可以使用扩展 WPF 工具包中的DateTimePicker。
有属性Value
,当您开始在此控件的文本框中输入值时,输入的值将自动分配给您的绑定属性。
例子:
XAML:
<extToolkit:DateTimePicker Value="{Binding MyDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
其中“extToolkit”是:
xmlns:extToolkit="http://schemas.xceed.com/wpf/xaml/toolkit"
视图模型类:
class MainViewModel : INotifyPropertyChanged
{
private DateTime _myDate;
public DateTime MyDate
{
get { return _myDate; }
set
{
_myDate = value;
OnPropertyChanged("MyDate");
// only for testing...
Console.WriteLine("value: " + value);
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}