【发布时间】:2011-12-09 21:14:07
【问题描述】:
如何让 DatePicker 控件更新视图模型而不失去焦点?
我希望能够运行此示例,输入 12/9/11 作为日期,单击窗口的关闭按钮并让 Debug.Assert 通过。如果我在关闭窗口之前切换到文本框,它可以正常工作。
<Window x:Class="DatePickerTest.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" Closing="MainWindow_OnClosing">
<StackPanel>
<DatePicker Name="TheDate" SelectedDate="{Binding Path=SelectedDate}" />
<TextBox/>
</StackPanel>
</Window>
using System;
using System.ComponentModel;
using System.Diagnostics;
namespace DatePickerTest
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
private void MainWindow_OnClosing(object sender, CancelEventArgs e)
{
var dataContext = DataContext as ViewModel;
Debug.Assert(dataContext.SelectedDate == new DateTime(2011, 12, 9));
}
}
public class ViewModel
{
public DateTime SelectedDate { get; set; }
}
}
【问题讨论】:
标签: wpf datepicker