自定义 WPF DatePickerTextBox 模板 [英] Custom WPF DatePickerTextBox Template

查看:40
本文介绍了自定义 WPF DatePickerTextBox 模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 DatePicker 控件中使用自定义 TextBox,但是我无法从弹出式日历中获取要绑定到 TextBox 的日期.我不想设置整个 DatePicker 的样式,除非我必须这样做,而且 DatePickerTextBox 有自己的控件,所以必须有一种方法可以只更改它.下面的代码是我的开始:

I am trying to use a custom TextBox in the DatePicker control, but I can't get the date to bind from the popup calendar to the TextBox. I don't want to have to style the entire DatePicker unless I have to, and the DatePickerTextBox has its own control, so there must be a way to only alter it. The code below is what I have as a start:

<Style TargetType="{x:Type DatePickerTextBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DatePickerTextBox}">
                <TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我可能没有正确进行绑定,或者 PART_TextBox 可能不正确,因为它不是 DatePicker 模板本身的一部分.

I may not be doing the binding correctly, or the PART_TextBox may not be right since it's not part of the DatePicker template itself.

有人请帮忙!:)

提前致谢!

推荐答案

试试这个:

<DatePicker>
    <DatePicker.Resources>
        <Style TargetType="{x:Type DatePickerTextBox}">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <TextBox x:Name="PART_TextBox" 
                                    Text="{Binding Path=SelectedDate, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DatePicker.Resources>
</DatePicker>

这篇关于自定义 WPF DatePickerTextBox 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆