首页 > 解决方案 > 客户 WPF 日期选择器样式会中断所选日期

问题描述

我正在尝试使用 WPF 日期选择器并使整个控件更大。类似的过程适用于日历弹出窗口,但由于某种原因,当为日期选择器这样做时,所选日期永远不会改变。有没有办法扩大日期选择器的所有组件?

页面控制:

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

样式:

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

标签: wpfwpf-style

解决方案


将 替换为绑定到实际TemplateBinding的双向绑定:SelectedDateDatePicker

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

推荐阅读