2010-09-03 187 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