2010-09-03 186 views
6

我想在DatePicker控件中使用自定义TextBox控件,但我无法获得日期从弹出日历绑定到TextBox。除非必须,否则我不想对整个DatePicker进行样式设计,并且DatePickerTextBox有自己的控制,所以必须有一种方法才能改变它。下面的代码是我作为一个开始:自定义WPF 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> 

我可能不会做正确的结合,或者因为它不是DatePicker模板本身的一部分PART_TextBox可能是不对的。

有人请帮忙! :)

在此先感谢!

回答

22

尝试了这一点:

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

明白了。非常感谢你! – 2010-09-03 14:02:15

+0

我对此有一个问题:它不遵守默认模板所做的短/长日期格式。 – dex3703 2011-09-30 21:50:09

+3

@ dex3707您可以将StringFormat添加到Textbinding并指定格式,例如 Dominik 2011-11-01 09:14:51

3

我知道这已经回答了很长一段时间了,而是直接绑定到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> 
+0

我已经花了几个小时,然后我发现你的答案被埋在了大量的问题和答案中。你的是最好的实现。好东西! – Tronald 2015-02-09 15:12:25