11

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,not the bound DateTimes. Does anyone know why this is happening?

This is what i have so far:

The MainWindow.xaml

<Window x:Class="CalendarListTest.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">
<Grid>
    <ItemsControl x:Name="calendarList">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Calendar DisplayDate="{Binding CurrentDate}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

** The place where the collection is assigned to the ItemsSource**

        private void Window_Loaded( object sender, RoutedEventArgs e )
    {
        CalendarList list = new CalendarList( );
        list.Add( new CalendarMonth( ) { CurrentDate = DateTime.Parse( "1.1.1979" ) } );
        list.Add( new CalendarMonth( ) { CurrentDate = DateTime.Parse( "1.2.1979" ) } );
        list.Add( new CalendarMonth( ) { CurrentDate = DateTime.Parse( "1.3.1979" ) } );

        calendarList.ItemsSource = list;
    }

The CalendarMonth ViewModel:

public class CalendarMonth
{
    private DateTime _currentDate;

    public DateTime CurrentDate
    {
        get { return _currentDate; }
        set { _currentDate = value; }
    }

}

And the Collection to bind to the ItemsControl:

public class CalendarList : ObservableCollection<CalendarMonth>
{
}

Now, the result:

enter image description here

Why is this happening?

edit: When providing <Calendar DisplayDate="{Binding CurrentDate, Mode=OneWay}" /> the binding works.

ovm
  • 2,452
  • 3
  • 28
  • 51

4 Answers4

17

Does this fit your needs?

<Calendar SelectedDate="{Binding Path=CurrentDate}"
          DisplayDate="{Binding Path=SelectedDate,
                                RelativeSource={RelativeSource Self}, 
                                Mode=OneWay}" />
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Markus Hütter
  • 7,796
  • 1
  • 36
  • 63
  • 1
    +1 great answer. I wonder why the Calender does not initialize itself like this... I ripped my hair trying to understand why the following wouldn't simply work: `` – Simon Mourier Jun 06 '14 at 16:05
5

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.

CodeNaked
  • 40,753
  • 6
  • 122
  • 148
1

Try binding to SelectedDate instead of DisplayDate. Alternatively, see if it works better if you set IsTodayHighlighted="false".

Chris Wenham
  • 23,679
  • 13
  • 59
  • 69
  • Requirement is that there can only be one SelectedDate per CalendarList. IsTodayHighlighted="False" doesn't help. – ovm Feb 21 '11 at 12:53
0

I did read somewhere that if you bind a list box to a set of selected items (such as from another list box), it only shows the first selected item as selected on the second list box. I'm not sure if this is relevant here, but it looks like it could be.

CodeMouse92
  • 6,840
  • 14
  • 73
  • 130