xamlcalendargriddatacontextdisplaymode

Xaml Calendar DisplayMode="Year" not working when used with Grid.Definitions and DataContext


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?


Solution

  • 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.