我知道这已经回答了很长一段时间了,而是直接绑定到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>
的“PART_TextBox”也没有必要,因为它不是DatePickerTextBox模板的一部分。该DatePickerTextBox包含唯一的部分是:
[TemplatePart(Name = DatePickerTextBox.ElementContentName, Type = typeof(ContentControl))]
public sealed partial class DatePickerTextBox : TextBox
private const string ElementContentName = "PART_Watermark";
和TextBoxBase继承...
[TemplatePart(Name = "PART_ContentHost", Type = typeof(FrameworkElement))]
public abstract class TextBoxBase : Control
internal const string ContentHostTemplateName = "PART_ContentHost";
替代解决方案: 如果您选择不使用TextBox和使用继承的一部分,你将能够在不更改控件的默认功能的情况下更改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>
明白了。非常感谢你! – 2010-09-03 14:02:15
我对此有一个问题:它不遵守默认模板所做的短/长日期格式。 – dex3703 2011-09-30 21:50:09
@ dex3707您可以将StringFormat添加到Textbinding并指定格式,例如 –
Dominik
2011-11-01 09:14:51