自定义WPF DatePickerTextBox模板

Ran*_*ary 9 .net wpf xaml textbox datepicker

我试图TextBoxDatePicker控件中使用自定义,但我无法将日期从弹出日历绑定到TextBox.DatePicker除非我必须,我不想要整个风格,并且它DatePickerTextBox有自己的控制权,所以必须有一种方法来改变它.下面的代码就是我的开头:

<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>
Run Code Online (Sandbox Code Playgroud)

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

有人请帮忙!:)

提前致谢!

ASa*_*nch 27

试试这个:

<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>
Run Code Online (Sandbox Code Playgroud)

  • @ dex3707您可以将StringFormat添加到Textbinding并指定格式,例如<TextBox x:Name ="PART_TextBox"Text ="{Binding Path = SelectedDate,RelativeSource = {RelativeSource AncestorType = {x:Type DatePicker}},StringFormat = d }"/> (3认同)

Ant*_* M. 5

我意识到这个问题已经得到解答很长时间了,但是直接绑定到DatePicker的 Text 属性将允许控件模板中的TextBox轻松遵循 DatePicker 提供的短/长格式

<DatePicker>
    <DatePicker.Resources>
        <Style TargetType="{x:Type DatePickerTextBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <TextBox Text="{Binding Text, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DatePicker.Resources>
</DatePicker>
Run Code Online (Sandbox Code Playgroud)

“PART_TextBox”也不是必需的,因为它不是DatePickerTextBox模板的一部分。DatePickerTextBox包含的唯一部分是:

[TemplatePart(Name = DatePickerTextBox.ElementContentName, Type = typeof(ContentControl))]
public sealed partial class DatePickerTextBox : TextBox

private const string ElementContentName = "PART_Watermark";
Run Code Online (Sandbox Code Playgroud)

并继承自TextBoxBase ...

[TemplatePart(Name = "PART_ContentHost", Type = typeof(FrameworkElement))] 
public abstract class TextBoxBase : Control

internal const string ContentHostTemplateName = "PART_ContentHost";
Run Code Online (Sandbox Code Playgroud)

替代解决方案: 如果您选择不使用TextBox并使用继承的 PART,您将能够更改DatePickerTextBox,而无需更改控件的默认功能。

<DatePicker>
    <DatePicker.Resources>
        <Style TargetType="{x:Type DatePickerTextBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Grid SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                            <Border BorderThickness="{TemplateBinding BorderThickness}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    Background="{TemplateBinding Background}"/>

                            <ScrollViewer Name="PART_ContentHost"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DatePicker.Resources>
</DatePicker>
Run Code Online (Sandbox Code Playgroud)