Demon Hunter
Demon Hunter

Reputation: 253

Selecteddate not updated in wpf datepicker when datepicker contains plain wpf textbox

I have extended a wpf date picker control and in styles I changed the template for date picker textbox style. I put a textbox in the control template of the datepickertextbox:

    <Style x:Key="DatePickerTextBoxStyle" TargetType="{x:Type DatePickerTextBox}">
    <Setter Property="Height" Value="20" />
    <Setter Property="Control.Template">
    <Setter.Value>
    <ControlTemplate>

    <TextBox x:Name="PART_TextBox"
    Style="{DynamicResource CalendarTextBoxStyle}"
    TabIndex="0"
    Text="{Binding Path=SelectedDate,
    StringFormat='d',
    ConverterCulture={x:Static glob:CultureInfo.CurrentCulture},
    RelativeSource={RelativeSource AncestorType={x:Type     maskedDatePickerLib:MaskedDatePicker}}}"
    TextWrapping="Wrap">

    </TextBox>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>

I have overridden the default template for date picker control only style for datepicker textbox is changed.

Now, the problem is when I select a date through calender it got displayed in text box through binding. but when I delete the date through backspace and if I try to select the same date again from calender, It does not get displayed in text box. When I investigated through snoop I saw the in SelectedDate property of DatePicker control the value I deleted is still there but in text box text propery value is empty as I deleted it. Kindly suggest.

Upvotes: 0

Views: 1311

Answers (1)

Sheridan
Sheridan

Reputation: 69987

If you look at the default ControlTemplate for the DatePicker control, you will see that it has four inner controls that have names that start with the PART_ prefix. (You can find the default ControlTemplate for the DatePicker control in the DatePicker Syles and Templates page on MSDN). Here is an example from the linked page:

<Button x:Name="PART_Button"
                Grid.Column="1"
                Foreground="{TemplateBinding Foreground}"
                Focusable="False"
                HorizontalAlignment="Left"
                Margin="3,0,3,0"
                Grid.Row="0"
                Style="{StaticResource DropDownButtonStyle}"
                VerticalAlignment="Top" />

The controls that have names that start with the PART_ prefix are internally used by the DatePicker class and so not having them in the new ControlTemplate will cause problems. The class asks for the controls by name and if they are not found, then default functionality cannot be completed.

Furthermore, you are trying to replace a DatePickerTextBox with all of its built in functionality for a normal TextBox and then wondering why the default functionality is not working properly... it's because you removed it and didn't replace it.

While I can see that you have tried to use the PART_ name it is clear that you don't fully understand what it does. The PART_TextBox control in the default ControlTemplate is of type DatePickerTextBox, but yours is of type TextBox, so whatever the DatePicker class normally does with that control, it now cannot do because it is of the wrong type.

Upvotes: 0

Related Questions