为何 wpf中DatePicker控件的SelectedDateChanged事件会执行两次。知道可能是跟事件路由机制有关,但是暂时不知道具体的解决办法!
并且他的SelectedDate属性只设置了一次。
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.dpkDay.SelectedDate = DateTime.Now;
}
private void dpkDay_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
if (e.Source is DatePicker)
{
DateTime? dtSelect = dpkDay.SelectedDate;
ExecQuerySVInfoLogic(dtSelect.Value);
}
e.Handled = true;
}
而且Handled 属性本人也设置了,应该也不会冒泡了,但是还是会执行两次,求高手赐教
并且他的SelectedDate属性只设置了一次。
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.dpkDay.SelectedDate = DateTime.Now;
}
private void dpkDay_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
if (e.Source is DatePicker)
{
DateTime? dtSelect = dpkDay.SelectedDate;
ExecQuerySVInfoLogic(dtSelect.Value);
}
e.Handled = true;
}
而且Handled 属性本人也设置了,应该也不会冒泡了,但是还是会执行两次,求高手赐教
解决方案
10
把this.dpkDay.SelectedDate = DateTime.Now;去掉还会执行吗
30
应该是你的Load函数执行了两次,所以 this.dpkDay.SelectedDate = DateTime.Now;会两次复制,导致SelectedDateChanged事件触发。你可以固定this.dpkDay.SelectedDate 的值,看会不会触发两次,假如是这个原因,那你就将this.dpkDay.SelectedDate = DateTime.Now放到构造函数里面吧。或找出Load执行两次的原因。