WPF Calendar - DisplayDate not working?

Nov 4, 2010 at 3:45 PM

Hi,

I'm using the Calendar as a control in my application and I am struggling at making "DisplayDate" work.

Basically my problem is that whatever I set DisplayDate to, the Calendar will always display today's date as a default. I took .NET 4.0's example from MSDN docs just to see, and I still have the same problem, try this:

<Calendar Margin="20" 
     SelectedDate="2/15/2009"
     DisplayDate="3/15/2009"
     DisplayDateStart="1/10/2009"
     DisplayDateEnd="4/18/2009"/>

According to this piece of code, it should display 3/15/2009. But if I run it here, Calendar is actually showing the month of April instead of March. This is because it's trying to display today's date (nov 2010) but gets blocked by "DisplayDateEnd". Remove that last property and the calendar will show Nov 2010 regardless of the DisplayDate.

Are there any workarounds to this? I am using the Feb 2010 release of the toolkit in VS2008/.NET 3.5.

Nov 4, 2010 at 7:24 PM
Edited Nov 4, 2010 at 7:24 PM

In fact, guess what? I was wrong, it works in a blank application.

But somehow, in the middle of my full application, it won't. This is what I'm using exactly:

<tk:Calendar DisplayDate="2010-01-01"/>

The actual displayed date is November 2010 in the calendar, using the line above.

The calendar is actually within a datatemplate, in a usercontrol:

<UserControl x:Class="PhaseView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:mkToolsProps="clr-namespace:mkTools.AttachedProperties"
    xmlns:mkConvert="clr-namespace:mkTools.Converters">

    <UserControl.Resources>
        <ResourceDictionary Source="/4 - Resources/Dictionaries/mkStyles.xaml" />
    </UserControl.Resources>

    <StackPanel>
         <ListBox ItemsSource="{Binding SelectedItem.Children}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Expander>
                        <Expander.Header>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="270" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <StackPanel Grid.Column="0">
                                    <TextBlock Text="{Binding Name}"/>
                                <TextBlock Text="{Binding Comment}" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" Grid.Column="1" >
                                    <Button Command="{Binding RenameCommand}" Style="{StaticResource EditButton}" />
                                    <Button Command="{Binding DeleteCommand}" Style="{StaticResource DeleteButton}" />
                                </StackPanel>
                            </Grid>
                        </Expander.Header>
                        <StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <StackPanel Orientation="Vertical">
                                    <TextBlock Text="From" />
                                    <tk:Calendar DisplayDate="2010-01-01"/>
                                    <TextBlock Text="{Binding PhaseStartText}" />
                                </StackPanel>
                                <StackPanel Orientation="Vertical">
                                    <TextBlock Text="To" />
                                    <TextBlock Text="{Binding PhaseEndText}" />
                                </StackPanel>
                            </StackPanel>
                        </StackPanel>
                    </Expander>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</UserControl>

The problem lies in <tk:Calendar> - it will always display the current month regardless of its "DisplayDate" value. Is this a bug, or something I'm doing wrong?
Also, the "mkStyles.XAML" has nothing related to calendars - it's only a collection of styles for buttons and listboxes.
Thanks!
Dec 3, 2010 at 9:40 PM
Edited Dec 3, 2010 at 10:06 PM

Did you find solution for this issue?  I have same problem.

 

If I just create a plain calendar in XAML and set DisplayDate directly with a date string, it works.  Example:

<toolkit:Calendar DisplayDate="3/1/2010" />

 

If I try to bind DisplayDate from within a DataTemplate, it never works.  Example:

<DataTemplate DataType="{x:Type sys:DateTime}">
     <toolkit:Calendar DisplayDate="{Binding Path=., Mode=OneWay}" />
</DataTemplate>

 

This always shows the current month in the Calendar and it's starting to make me mad!

 

Using this DataTemplate, it is my goal to make ListBox full of Calendars like this:

 

 

<ListBox Padding="0" Margin="0" Background="Transparent" BorderBrush="{x:Null}" BorderThickness="0" HorizontalAlignment="Center"
        ScrollViewer.HorizontalScrollBarVisibility="Disabled"
        ScrollViewer.VerticalScrollBarVisibility="Disabled"
        FocusVisualStyle="{x:Null}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.Items>
            <sys:DateTime>11/1/2010</sys:DateTime>
            <sys:DateTime>12/1/2010</sys:DateTime>
            <sys:DateTime>1/1/2011</sys:DateTime>
            <sys:DateTime>2/1/2011</sys:DateTime>
            <sys:DateTime>3/1/2011</sys:DateTime>
        </ListBox.Items>
    </ListBox>

 

To exaggerate the problem I have modified the DataTemplate like this:

<DataTemplate DataType="{x:Type sys:DateTime}">
            <StackPanel Orientation="Vertical">
                <toolkit:Calendar Background="Transparent" DisplayDate="{Binding Path=.}" />
                <TextBlock Text="{Binding Path=.}"/>
            </StackPanel>
        </DataTemplate>

 Now, it shows the correct date in the TextBlock below the calendar.  But the Calendar will only show the current month if I try to set the DisplayDate from within the DataTemplate!  Please help!