WPF: binding viewmodel property of type DateTime to Calendar inside ItemsControl

后端 未结 4 574
说谎
说谎 2021-02-08 07:55

i have a problem with WPF Binding. I want to bind a list of Months to a ItemsControl that shows a Calendar Control for each month. But each rendered Calendar shows DateTime.Now,

4条回答
  •  长情又很酷
    2021-02-08 08:13

    The issue appears to be with how the Calendar initializes the DisplayDate property. It currently does it like this:

    public Calendar() {
        // ...
        base.SetCurrentValueInternal(DisplayDateProperty, DateTime.Today);
    }
    

    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:

    public class MyCalendar : Calendar {
        public MyCalendar() {
            this.ClearValue(DisplayDateProperty);
        }
    }
    

    Or you could establish the binding at a later time (i.e. when the Calendar is loaded) using an event handler or attached behavior.

提交回复
热议问题