我在MVVM应用程序中使用常规WPF日历(System.Windows.Controls.Calendar)。 日历一次显示一个月,用户将选择一天。 日历显示下个月的几天在底部,这很好。
问题是 ,如果我从下个月(例如下个月的第二个月)选择一个日期,那么日历首先更新数据绑定与此日期,然后立即在本月1日再次更新它!
这种情况发生在我选择的下个月的所有日期。
日历的SelectedDate和DisplayDate属性是绑定到视图模型的Date属性的数据。
<Calendar SelectedDate="{Binding Date}" DisplayDate="{Binding Date}" />视图模型看起来像这样
public class CalendarProblemDemoViewModel : INotifyPropertyChanged { private DateTime _date; public CalendarProblemDemoViewModel() { _date = DateTime.Today; } public DateTime Date { get { return _date; } set { _date = value; Debug.Write($"new date {_date}\n"); OnPropertyChanged(nameof(Date)); } } // Note: other code omitted, see the github project }我不明白为什么会这样?
I am using a regular WPF Calendar (System.Windows.Controls.Calendar) in an MVVM application. The calendar shows a month at a time, and the user will pick a single day. The calendar shows a few days of the next month at the bottom, which is fine.
The problem is that if I pick a date from the next month (eg the 2nd of the next month) then Calendar first updates the data binding with this date, and then immediately updates it again with the 1st of the month!
This happens for all dates in the next month that I select.
The calendar's SelectedDate and DisplayDate properties are data bound to the view model's Date property.
<Calendar SelectedDate="{Binding Date}" DisplayDate="{Binding Date}" />The view model looks like this
public class CalendarProblemDemoViewModel : INotifyPropertyChanged { private DateTime _date; public CalendarProblemDemoViewModel() { _date = DateTime.Today; } public DateTime Date { get { return _date; } set { _date = value; Debug.Write($"new date {_date}\n"); OnPropertyChanged(nameof(Date)); } } // Note: other code omitted, see the github project }I don't understand why this is happening?
最满意答案
您不应将SelectedDate和DisplayDate绑定到同一属性Date 。
You should not bind both SelectedDate and DisplayDate to the same property Date.
更多推荐
发布评论