Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to use a custom TextBox in the DatePicker control, but I can't get the date to bind from the popup calendar to the TextBox. I don't want to have to style the entire DatePicker unless I have to, and the DatePickerTextBox has its own control, so there must be a way to only alter it. The code below is what I have as a start:

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

I may not be doing the binding correctly, or the PART_TextBox may not be right since it's not part of the DatePicker template itself.

Someone please help! :)

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
788 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this out:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...