0

I am under a situation where i need to develop DatePicker where the calendar must disappear on mouse leave event, only textbox with the selected date from calendar must be visible and calendar part must disappear, and calendar part must appear only on mouse enter of DatePicker so that the user will be able to select the date and immediately disappear as he selected and lost the focus.

I don't know visibility of which control of DatePicker has to be set to Visible or Hide. I tried several things from the DatePicker template which i found on the link given in resource part of my xaml, by setting the visibility to hidden or Visible but nothing worked. For example CalendarStyle below

 <DatePicker x:Name="MyDatePicker" Margin="0,0,305,28" 
  CalendarStyle="{StaticResource CalendarName}">
 </DatePicker> 

Entire control style with my xaml code is here :

<Window x:Class="calendarHideTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <!--Here i kept the style of DatePicker written at link https://learn.microsoft.com/en-us/dotnet/framework/wpf/controls/calendar-styles-and-templates-->                
    </Window.Resources>
    <StackPanel>
        <DatePicker x:Name="MyDatePicker" 
                    Margin="0,0,305,28" 
                    CalendarStyle="{StaticResource CalendarName}">
        </DatePicker>
    </StackPanel>
</Window>

I want to know, Which subcontrol of datepicker i have to set to visibility "Hidden" to make the calendar part disappear ?

1 Answer 1

0

If I understood you correctly you can use this approach.

<DatePicker x:Name="datePicker" Width="100" Height="50" MouseEnter="DatePicker_OnMouseEnter" MouseLeave="DatePicker_OnMouseLeave" Loaded="DatePicker_OnLoaded"/>

And in code behind.

...
    private void DatePicker_OnMouseEnter(object sender, MouseEventArgs e)
    {
        ChangeCalendarButtonVisibility(Visibility.Visible);
    }

    private void DatePicker_OnMouseLeave(object sender, MouseEventArgs e)
    {
        ChangeCalendarButtonVisibility(Visibility.Collapsed);
    }

    private void DatePicker_OnLoaded(object sender, RoutedEventArgs e)
    {
        //We use loaded event to hide button in initial state
        ChangeCalendarButtonVisibility(Visibility.Collapsed);
    }

    private Button GetCalendarButton()
    {
        var template = datePicker.Template;
        var button = (Button)template.FindName("PART_Button", datePicker);
        return button;
    }

    private void ChangeCalendarButtonVisibility(Visibility visibility)
    {
        var button = GetCalendarButton();
        if (button != null)
        {
            button.Visibility = visibility;
        }
    }
...

You don't even need to store DatePicker style in your control resources.

Not the answer you're looking for? Browse other questions tagged or ask your own question.