Kevin DiTraglia
Kevin DiTraglia

Reputation: 26058

Overriding Calendar Style in DatePicker

So I wrote this code a while ago and it used to work fine, but recently for whatever reason now it doesn't work at all. Here's the xaml...

<DatePicker SelectedDate="{Binding Criteria}" IsEnabled="{Binding IsEnabled}" Width="130" HorizontalAlignment="Right">
    <DatePicker.CommandBindings>
        <CommandBinding Command="controls:DateCommands.SelectToday" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed"/>
    </DatePicker.CommandBindings>
    <DatePicker.CalendarStyle>
        <Style TargetType="{x:Type Calendar}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Calendar}">
                        <TabControl>
                            <TabItem Header="Calender">
                                <CalendarItem Background="{TemplateBinding Control.Background}" BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="{TemplateBinding Control.BorderThickness}" Style="{TemplateBinding Calendar.CalendarItemStyle}" />
                            </TabItem>
                            <TabItem Header="Relative Time">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <Button Grid.Column="0" Command="controls:DateCommands.SelectToday" CommandParameter="Today" Content="Today" />
                                    <Button Grid.Column="1" Command="controls:DateCommands.SelectToday" CommandParameter="Yesterday" Content="Yesterday" />
                                </Grid>
                            </TabItem>
                        </TabControl>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DatePicker.CalendarStyle>
</DatePicker>

Basically I am overriding the calendar of the date picker and putting in a tab control that allows the user to select a relative time macro rather than an actual time (causing the generated query to always use that relative time). This used to work fine, but now whenever I try to select from the calendar nothing happens and no values are ever populated in the box, or in the backing property.

I took the code out and dumped it into a new project to experiment and couldn't get this simple case to even work...

<DatePicker Width="130" HorizontalAlignment="Right">
    <DatePicker.CalendarStyle>
        <Style TargetType="{x:Type Calendar}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Calendar}">
                        <CalendarItem Background="{TemplateBinding Control.Background}" BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="{TemplateBinding Control.BorderThickness}" Style="{TemplateBinding Calendar.CalendarItemStyle}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DatePicker.CalendarStyle>
</DatePicker>

This code does the same thing, whenever you try to pick a date from the calendar, nothing happens. This used to work, and to my knowledge nothing has changed, anybody have any insight on what's going on, or perhaps a better implementation of what I am trying to do?

Upvotes: 3

Views: 15061

Answers (1)

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26058

I figured it out. For whatever reason this part is important for this all to function...

<CalendarItem x:Name="PART_CalendarItem"... />

I'm not quite sure why, but I do remember removing it at some point thinking it wasn't doing anything, but without that line the date picked never populates the text box or the backing property.

Upvotes: 4

Related Questions