5

I'm using the calendar in DisplayMode="Year" and this.DataContext = new SampleModel(); so I have a access to the properties of the Model. However the Calendar is rendered wrong (see screenshot)

The code comes down to: Xaml:

<Window x:Class="Excel2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="600" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="250"></ColumnDefinition>
            <ColumnDefinition Width="2*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="220"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>

        <Calendar DisplayMode="Year"></Calendar>
    </Grid>
</Window>

Code behind:

using ....

namespace Excel2
{
    class SampleModel
    {
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {            
            InitializeComponent();
            this.DataContext = new SampleModel();
        }
    }
}

Result: XAML calender Displaymode=Year error As you can see, the calender is renderd without shoowing any year informationn.

If I dont use Grid definitions, Displaymode=Year or this.DataContext =... everything is rendered correctly.

Is this a bug in XAML?

Stefan
  • 14,826
  • 17
  • 80
  • 143

1 Answers1

5

As this question's been asked for over an year now and still doesn't have any accepted answer so i would like to contribute on how i got rid of this bug.

I changed my xaml to:

 <Calendar Grid.Row="0" Grid.Column="3" x:Name="_calendar" DisplayModeChanged="_calendar_DisplayModeChanged" Loaded="_calendar_OnLoaded"
                          DisplayDate="{Binding SelectedMonth, UpdateSourceTrigger=PropertyChanged}" DisplayMode="Month" />

 //Setting DisplayMode="Month" in xaml and will change it back to "Year" in code behind. so my codebehind code is


    private void _calendar_DisplayModeChanged(object sender, CalendarModeChangedEventArgs e)
    {
        _calendar.DisplayMode = CalendarMode.Year;
    }

    private void _calendar_OnLoaded(object sender, RoutedEventArgs e)
    {
        _calendar.DisplayMode = CalendarMode.Year;
    }

Loaded event is required to change the display mode for the first time to Year and DisplayModeChanged event is required to change it on subsequent calls when there's selection changed.

I hope it helps someone in future.

Ali Baig
  • 3,819
  • 4
  • 34
  • 47
  • I don't understand why you had no upvote for this answer. I was struggling to achieve this monthpicker and you were the only one who had a working solution... Thanks a lot! – Speuline Nov 08 '16 at 09:37
  • 1
    Glad it helped @Speuline – Ali Baig Nov 08 '16 at 15:40
  • I've another issue with your code if you have a few minutes: http://stackoverflow.com/questions/40491255/issue-monthpicker-size-with-mvvm-light-toolkit – Speuline Nov 08 '16 at 15:57
  • after a long time, and not working with xaml anymore, I noticed that I didn't reward that answer. take my gratitude – Stefan Nov 08 '16 at 21:14