Toggle datepicker calendar when clicking/touching PART_Textbox
datepicker, wpf, xaml
Solution
Forget about the `EventTrigger` because it doesn't give you enough control. Instead of using that, just add a `bool IsDropDownOpen` property into your view model or code behind and bind it to the `DatePicker.IsDropDownOpen` property. This way, you are free to open and close the `Popup` anytime you want and/or in response to any single, or collection of events. For example:
public void TextBoxPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
IsDropDownOpen = true;
}
private void TextBoxPreviewTouchDown(object sender, TouchEventArgs e)
{
IsDropDownOpen = true;
}
private void DatePickerSelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
IsDropDownOpen = false;
}
Problem
I am writing a datepicker style in xaml The calendar popup is toggled by the calendar button next to the textbox of the datepicker I want the calendar also toggling when clicking/touching the textbox When adding an event trigger to the datepicker style, the calendar toggles when touching the textbox, but only shows on a mouse click and stays open, when clicking/touching the datepicker calendar button, the calendar opens and closes immediately. ``` <EventTrigger RoutedEvent="TextBox.PreviewMouseDown"> <EventTrigger.Actions> <BeginStoryboard x:Name="myBeginStoryboard"> <Storyboard x:Name="myStoryboard"> <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsDropDownOpen"> <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="True" /> </BooleanAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> ``` My questions: - Is there a better way to achieve the desired behavior than using an event trigger? - How can the calendar show and hide when touching the control while the event trigger only sets the calendar visible? - Is it possible to assign the inverted "IsDropDownOpen" value to this property in xaml? - or is it possible to use a conditional statement (if?) in xaml to handle both hiding and showing?