0

Can anybody give me a ControlTemplate for DatePickerTextBox?

I have visited MSDN. There I am not able to find ControlTemplate for DatePickerTextBox. However I was able to find ControlTemplate for DatePicker. There I just found that DatePickerTextBox has two different parts namely : PART_Watermark and PART_ContentElement. But I am not able to find a ControlTemplate for DatePickerTextBox.

Vishal
  • 6,238
  • 10
  • 82
  • 158
  • 1
    http://stackoverflow.com/questions/3636310/custom-wpf-datepickertextbox-template-help – Sajeetharan Aug 31 '14 at 14:48
  • @Sajeetharan I have visited that link before asking this question. There I cannot find the full ControlTemplate. Both of the above mentioned parts namely PART_Watermark and PART_ContentElement are not present in that Template. – Vishal Aug 31 '14 at 14:50
  • @Sajeetharan Also, my primary need here is to change the Watermarked text as well as the Date separator from `/` to `.` – Vishal Aug 31 '14 at 15:00

1 Answers1

3

Here goes default template for DatePickerTextBox:

<ControlTemplate TargetType="DatePickerTextBox">
    <Grid>
        <Grid.Resources>
            <SolidColorBrush
                x:Key="G">#FFAAAAAA</SolidColorBrush>
        </Grid.Resources>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup
                Name="CommonStates" />
            <VisualStateGroup
                Name="WatermarkStates" />
            <VisualStateGroup
                Name="FocusStates" />
        </VisualStateManager.VisualStateGroups>
        <Border
            BorderThickness="{TemplateBinding Border.BorderThickness}"
            Padding="{TemplateBinding Control.Padding}"
            CornerRadius="1,1,1,1"
            BorderBrush="{TemplateBinding Border.BorderBrush}"
            Background="{TemplateBinding Panel.Background}"
            Name="Border"
            Opacity="1">
            <Grid
                Name="WatermarkContent"
                HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}">
                <Border
                    BorderThickness="1,1,1,1"
                    BorderBrush="#FFFFFFFF"
                    Name="ContentElement" />
                <Border
                    BorderThickness="1,1,1,1"
                    BorderBrush="#FFFFFFFF"
                    Name="watermark_decorator">
                    <ContentControl
                        Padding="2,2,2,2"
                        Name="PART_Watermark"
                        Opacity="0"
                        IsHitTestVisible="False"
                        Focusable="False" />
                </Border>
                <ScrollViewer
                    HorizontalContentAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                    VerticalContentAlignment="{TemplateBinding Control.VerticalContentAlignment}"
                    Name="PART_ContentHost"
                    Margin="0,0,0,0" />
                <Border
                    CornerRadius="1,1,1,1"
                    BorderBrush="#FF45D6FA"
                    Name="FocusVisual"
                    Opacity="0"
                    IsHitTestVisible="False" />
            </Grid>
        </Border>
    </Grid>
</ControlTemplate>
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • If the above mentioned template is default, then why can't I find the default watermark text saying `Select a date` in that template? – Vishal Aug 31 '14 at 15:16
  • 1
    When you add `DatePickerTextBox` you get default text `` (atleast in windows 8) and that comes from `VisualStateGroup` called `WatermarkStates` which must be defined in theme style in `PresentationFramework` assembly. – Rohit Vats Aug 31 '14 at 15:21
  • There are two separate controls `DatePickerTextBox` and one `DatePicker`. Which one you want? – Rohit Vats Aug 31 '14 at 15:24
  • I want to use DatePicker control. I think DatePicker uses DatePickerTextBox in its control Template as mentioned here: http://msdn.microsoft.com/en-us/library/ff468215(v=vs.110).aspx – Vishal Aug 31 '14 at 15:25
  • So, I need to change the watermark text and Date Separator for DatePickerTextBox. – Vishal Aug 31 '14 at 15:26
  • Most likely it comes from VisualStates. However for setting custom text, have you tried the solution [here](http://stackoverflow.com/a/1102073/632337)? – Rohit Vats Aug 31 '14 at 15:38
  • OK, I tried that. There he mentioned that I should use Watermark Property instead of Text Property. But I cannot find Watermark Property on DatePickerTextBox. – Vishal Aug 31 '14 at 15:48
  • That's readonly property like said in the answer. Setting Text doesn't work for you? – Rohit Vats Aug 31 '14 at 15:50
  • Yes setting text works for me but The text is then selectable. – Vishal Aug 31 '14 at 15:52
  • Still I am marking this as answer as my needs are satisfied. I wanted a template and you provided me one. Plus point of your provided template is that it completely removes the watermarked text. Thanks Rohit. – Vishal Aug 31 '14 at 16:08
  • 2
    Watermark property is readOnly so you cannot set it from outside. However, you can create attached beahvior and set the content manually for waterMarkTextBox. This will work for you - [Set watermark from XAML](http://stackoverflow.com/a/5887276/632337). – Rohit Vats Aug 31 '14 at 16:14
  • 2
    Or create your own DatePicker control and override `OnApplyTemplate` like described [here](http://blogs.msdn.com/b/silverlight_sdk/archive/2010/05/06/changing-the-watermark-text-in-a-datepicker-control.aspx). In both cases jest is to set manually content value of WaterMarkTextBox. – Rohit Vats Aug 31 '14 at 16:16
  • And 1 more thing can you say how do I change DateSeparator from `/` to `.`? – Vishal Aug 31 '14 at 16:16
  • `DatePicker` control picks the pattern from `Thread.CurrentThread.CurrentCulture`. You need to override CurrentCulture DateTimeFormat but that will change date format for all controls and dateTime object. Otherwise you want it for your own control, try the attached behavior like described here - (http://stackoverflow.com/a/12993259/632337) (not tested though). – Rohit Vats Aug 31 '14 at 16:24