James F
James F

Reputation: 565

Customer WPF datepicker style breaks selected date

I'm trying to take a WPF datepicker and just make the whole control larger. A similar process works for the Calendar popup, but for some reason, when doing it this way for the datepicker, the selected date never changes. Is there a way to scale up all components of a datepicker?

The control on page:

        <DatePicker Grid.Column="0" SelectedDateFormat="Short" x:Name="StartDate"
                CalendarStyle="{StaticResource resizedCalendarItem}" Style="{StaticResource resizedCalendarButton}" />

The style:

            <Style x:Key="resizedCalendarButton" TargetType="{x:Type DatePicker}" BasedOn="{StaticResource {x:Type DatePicker}}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DatePicker}">
                        <Viewbox HorizontalAlignment="Center" Width="150">
                            <DatePicker SelectedDate="{TemplateBinding SelectedDate}" />
                        </Viewbox>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Upvotes: 0

Views: 92

Answers (1)

mm8
mm8

Reputation: 169200

Replace the TemplateBinding with a two-way binding that binds to the SelectedDate of the actual DatePicker:

<ControlTemplate TargetType="{x:Type DatePicker}">
    <Viewbox HorizontalAlignment="Center" Width="150">
        <DatePicker SelectedDate="{Binding SelectedDate, 
            RelativeSource={RelativeSource AncestorType=DatePicker}}" />
    </Viewbox>
</ControlTemplate>

Upvotes: 1

Related Questions