Calendar.DisplayDate TwoWay Binding 传播回默认值
我有 2 个日历,我想显示本月和下个月的日历,但两者都具有相同的选定日期。我在模型中使用 2 个简单的 ShownDate 属性来存储和计算当前月份和下个月。
<Calendar SelectedDate="{Binding Date, Mode=TwoWay}"
DisplayDate="{Binding ShownDate, Mode=TwoWay}"
Margin="4" AllowDrop="True" />
<Calendar SelectedDate="{Binding Date, Mode=TwoWay}"
DisplayDate="{Binding ShownDate2, Mode=TwoWay}"
Margin="4" AllowDrop="True" />
我意识到,在显示它之前,我在 ShownDate2 属性设置当前月份 (UpdateSource) 中得到了对模型的传播,因此忘记了我的默认值(必须是下个月,而不是当前)。它发生在对我的值的任何查询之前(UpdateTarget 稍后发生)。
这是 Calendar.DisplayDate 绑定行为中的错误吗?
请注意,所有这些都包含在由 ContentPresenter 绘制的 DataTemplate 中,但我认为这并不重要。
更新: 现在我确信 DataTemplates 确实很重要,但无法在简单的项目中重现该错误。我还是迷路了。
I have 2 calendars which I want to show this month and next one, but both with same selected date. I use 2 simple properties ShownDate in my model to store and calculate current and next month.
<Calendar SelectedDate="{Binding Date, Mode=TwoWay}"
DisplayDate="{Binding ShownDate, Mode=TwoWay}"
Margin="4" AllowDrop="True" />
<Calendar SelectedDate="{Binding Date, Mode=TwoWay}"
DisplayDate="{Binding ShownDate2, Mode=TwoWay}"
Margin="4" AllowDrop="True" />
I realized that before showing it, I get a propagation to my model in the ShownDate2 property setting current month (UpdateSource), so forgotting my default values (it must be next month, not current). And it happens before any query for my value (UpdateTarget occurs later).
Is this a bug in Calendar.DisplayDate binding behavior?
Note that all this is contained in a DataTemplate being drawn by a ContentPresenter, but I think it doesn't matter.
UPDATE:
Now I'm sure DataTemplates do matter, but can't reproduce the bug in a simple project. I'm still lost.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信这可能是 WPF 的重复项:将 DateTime 类型的 viewmodel 属性绑定到 ItemsControl 内的 Calendar,但总结一下:
问题似乎在于 Calendar 如何初始化 DisplayDate 属性。目前它是这样做的:
看来,即使 DisplayDate 在建立绑定之前被初始化,它仍然会被推回到绑定源,就像它是在绑定之后设置的一样。这很可能是一个错误。
您可以使用以下方法解决它:
或者您可以稍后建立绑定(即加载日历时)。
I believe this might be a duplicate of WPF: binding viewmodel property of type DateTime to Calendar inside ItemsControl, but to summerize:
The issue appears to be with how the Calendar initializes the DisplayDate property. It currently does it like this:
It appears that even though the DisplayDate is being initialized before the binding is established, it will still be pushed back to the binding source as if it were set after. This is most likely a bug.
You can work around it using something like:
Or you could establish the binding at a later time (i.e. when the Calendar is loaded).