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

calendardatetimeitemscontrolwpf

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.

Best Answer

Does this fit your needs?

<Calendar SelectedDate="{Binding Path=CurrentDate}"
          DisplayDate="{Binding Path=SelectedDate,
                                RelativeSource={RelativeSource Self}, 
                                Mode=OneWay}" />
Related Topic