locked
Calendar NOT losing focus WPF 4.0 RRS feed

  • Question

  • I have a WPF 4.0 Calendar control (Selection Mode: MultipleRange) and a button in a grid.

    After selecting dates in the calendar, when I click on the button, the calendar is not losing its focus.  i.e., It leads me to click twice on the button to fire the button_click event.

    Is that a known bug? I binged my level best but not finding any answer. I will appreciate if someone could solve it.

    Thank you.

     


    sgrajan
    Saturday, October 2, 2010 5:05 AM

Answers

  • I have a WPF 4.0 Calendar control (Selection Mode: MultipleRange) and a button in a grid.

    After selecting dates in the calendar, when I click on the button, the calendar is not losing its focus.  i.e., It leads me to click twice on the button to fire the button_click event.

    Is that a known bug? I binged my level best but not finding any answer. I will appreciate if someone could solve it.

    Thank you.

     


    sgrajan

    My solution to this problem was to insert

        protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
        {
          base.OnPreviewMouseUp(e);
          if (Mouse.Captured is Calendar || Mouse.Captured is System.Windows.Controls.Primitives.CalendarItem)
          {
            Mouse.Capture(null);
          }
        }
    

    into the main form, but it's probably overkill.

    • Marked as answer by Min Zhu Monday, November 15, 2010 1:46 AM
    Wednesday, November 10, 2010 6:34 AM
  • Hi Sgrajan,

    This is because the first click is not actually captured by the Button. The mouse is captured by the Calendar control waiting for this mouse event to close its popup window. You can click anywhere outside of the Popup window to close it.

    The second click functions as normal.

    So this is not a bug. It is an expected behavior.

    Hope this helps.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Min Zhu Monday, October 11, 2010 1:55 AM
    Wednesday, October 6, 2010 9:38 AM
  • You guys are fighting an uphill battle on this because as Min said, the Calendar popup has captured the mouse (much like a ComboBox popup list does when it is open). One possible workaround would be to hook the PreviewMouseDownOutsideCapturedElement event on the Calendar, check the mouse coordinates on the event callback, attempt to work out which control the mouse was over based on that location, and programmatically give it focus. To hook the event do something like this:

    Mouse.AddPreviewMouseDownOutsideCapturedElementHandler( calendar, new MouseButtonEventHandler( MouseDownOutsideCapturedElementHandler ) );
    
    

    Reminder: If someones post answers your question, please click "Mark as Answer". If a post provides information you find useful, please click "Vote as Helpful".
    • Proposed as answer by matte303 Tuesday, October 19, 2010 12:13 AM
    • Marked as answer by Min Zhu Tuesday, October 19, 2010 1:05 AM
    Tuesday, October 19, 2010 12:10 AM

All replies

  • Hi Sgrajan,

    This is because the first click is not actually captured by the Button. The mouse is captured by the Calendar control waiting for this mouse event to close its popup window. You can click anywhere outside of the Popup window to close it.

    The second click functions as normal.

    So this is not a bug. It is an expected behavior.

    Hope this helps.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Min Zhu Monday, October 11, 2010 1:55 AM
    Wednesday, October 6, 2010 9:38 AM
  • Hi,

    I have the same behavior with a TextBox:

    <Window x:Class="MyApplication.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">
        <Grid>
            <Calendar Height="170"
                      Width="180" />
            <TextBox Height="23"
                     Width="120"
                     Margin="189,246,194,42" />
        </Grid>
    </Window>

    The issue is that I would like my user to be able to switch from the calendar to the TextBox with one click as the user will complain if he has to click twice to get the TextBox to gain focus.

    How do I achieve a single click behavior ?

    Regards,

    Yann Randin

    Monday, October 18, 2010 1:29 PM
  • You guys are fighting an uphill battle on this because as Min said, the Calendar popup has captured the mouse (much like a ComboBox popup list does when it is open). One possible workaround would be to hook the PreviewMouseDownOutsideCapturedElement event on the Calendar, check the mouse coordinates on the event callback, attempt to work out which control the mouse was over based on that location, and programmatically give it focus. To hook the event do something like this:

    Mouse.AddPreviewMouseDownOutsideCapturedElementHandler( calendar, new MouseButtonEventHandler( MouseDownOutsideCapturedElementHandler ) );
    
    

    Reminder: If someones post answers your question, please click "Mark as Answer". If a post provides information you find useful, please click "Vote as Helpful".
    • Proposed as answer by matte303 Tuesday, October 19, 2010 12:13 AM
    • Marked as answer by Min Zhu Tuesday, October 19, 2010 1:05 AM
    Tuesday, October 19, 2010 12:10 AM
  • Hi,


    >This is because the first click is not actually captured by the Button. The mouse is captured by the Calendar control waiting for this mouse event >to close its popup window. You can click anywhere outside of the Popup window to close it.

    >The second click functions as normal.

    >So this is not a bug. It is an expected behavior.

    i think you are right, if we are talking about the DatePicker. The Calendar acts as a popup window in the DatePicker and everything works as expected there.

    But we are talking about the Calendar control itself, which is not a popup window. I really don't think that it is expected behaviour, that the user has to click somewhere else to get his mouse-focus back for interaction with other controls on the same level.

    Btw. this problem has already been reported as a bug for the CalendarControl of the WPF-Toolkit in  Dec 2009:

    http://wpf.codeplex.com/workitem/11194

    Regards,

    THamTHon

    • Proposed as answer by Anonymous99999 Wednesday, November 10, 2010 5:42 AM
    Tuesday, October 19, 2010 9:11 AM
  • I have a WPF 4.0 Calendar control (Selection Mode: MultipleRange) and a button in a grid.

    After selecting dates in the calendar, when I click on the button, the calendar is not losing its focus.  i.e., It leads me to click twice on the button to fire the button_click event.

    Is that a known bug? I binged my level best but not finding any answer. I will appreciate if someone could solve it.

    Thank you.

     


    sgrajan

    My solution to this problem was to insert

        protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
        {
          base.OnPreviewMouseUp(e);
          if (Mouse.Captured is Calendar || Mouse.Captured is System.Windows.Controls.Primitives.CalendarItem)
          {
            Mouse.Capture(null);
          }
        }
    

    into the main form, but it's probably overkill.

    • Marked as answer by Min Zhu Monday, November 15, 2010 1:46 AM
    Wednesday, November 10, 2010 6:34 AM
  • So this is not a bug. It is an expected behavior.

    This is most certainly a bug. I can reproduce it in the simlest of windows with just a calendar control and a single button. Any time the calendar control is clicked the buttons will need to be clicked twice to get them to respond. If this is expected behaviour it is VERY odd expected behaviour. I encountered it in the window below. This is just a window with a calendar, ok and cancel button. Any time the calendar has focus the buttons need to be clicked twice to respond. The solution from Anonymouse9999 worked for me altough it didn't solve the IsDefault problem.

    <Window x:Class="TestVisualLogicalTree.DatePopupWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            ResizeMode="NoResize"
            Title="Select Date"
            ShowInTaskbar="False"
            SizeToContent="WidthAndHeight">
       
        <DockPanel>
            <StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom" HorizontalAlignment="Right">
                <Button Margin="6" Padding="4" IsDefault="True" Click="Button_Click" Name="okButton">OK</Button>
                <Button Margin="6" Padding="4" IsCancel="True">Cancel</Button>
            </StackPanel>
            <Calendar BorderBrush="{x:Null}" Name="mainCalendar" MouseDoubleClick="mainCalendar_MouseDoubleClick" ></Calendar>
        </DockPanel>
    </Window>

     


    Tuesday, June 21, 2011 5:17 AM
  • Genius! wish i would have found this solution a while ago. i just put this into the previewmouseup event on my calendar on my application as a more quick and dirty

    Private Sub MyCal_PreviewMouseUp(sender As System.Object, e As System.Windows.Input.MouseButtonEventArgs) Handles MyCal.PreviewMouseUp
            If TypeOf Mouse.Captured Is Calendar OrElse TypeOf Mouse.Captured Is Primitives.CalendarItem Then
                Mouse.Capture(Nothing)
            End If
        End Sub

    it's pretty annoying to have to click twice after you choose your date to hit another control on the application, this fixes that and i'm sure will make some users less angry at me B)
    Friday, July 13, 2012 6:34 PM
  • This solution works fine for me. I had the same problem with the calendar control to click twice for a button to click and the customer did not like that at all.
    Friday, August 23, 2013 6:40 AM
  • There is on overkill, only "open fire" and "reload" -- thank you for this, it's just exactly what I needed!
    Sunday, September 6, 2015 7:40 PM
  • It doesn't really help, but I still appreciate having the explanation.  :)
    Sunday, September 6, 2015 7:41 PM
  • I know this question is pretty old, but i ran in exactly the same problem today.

    Seems like it's still not fixed and as I'm in an MVVM Application i thought to share my solution, which is based on an attached property, so you can keep the code-behind clean

    public static class CalendarCaptureFix
    {
    	public static bool GetFixCapture(DependencyObject obj)
    	{
    		return (bool)obj.GetValue(FixCaptureProperty);
    	}
    
    	public static void SetFixCapture(DependencyObject obj, bool value)
    	{
    		obj.SetValue(FixCaptureProperty, value);
    	}
    
    	// Using a DependencyProperty as the backing store for FixCapture.  This enables animation, styling, binding, etc...
    	public static readonly DependencyProperty FixCaptureProperty =
    		DependencyProperty.RegisterAttached("FixCapture", typeof(bool), typeof(Calendar), new PropertyMetadata(false,(s,e) =>
    		{
    			Calendar cal = s as Calendar;
    			if(cal != null)
    			{
    				if ((bool)e.NewValue)
    				{
    					cal.PreviewMouseUp += Cal_PreviewMouseUp;
    				}
    				else
    				{
    					cal.PreviewMouseUp -= Cal_PreviewMouseUp;
    				}
    			}
    		}));
    
    	private static void Cal_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    	{
    		Mouse.Capture(null);
    	}
    }

    and in the xaml it's used like this:

    <Calendar classes:CalendarCaptureFix.FixCapture="True" />

    Wednesday, September 27, 2017 9:22 AM
  • @FastJack2

    Changed to SelectedDatesChanged event so month navigation will work, thanks for your code has been very helpful.

    Thursday, May 10, 2018 2:44 AM
  • @Edwyn Amador Came across @FastJack2 fix. Worked great to automatically lose focus from my datepicker. However, like you said, I can no longer navigate between months or years. I saw you mentioned you fixed this problem using SelectedDatesChanged event. How did you implement this?  
    • Edited by cdubw Monday, October 15, 2018 5:35 PM
    Monday, October 15, 2018 5:03 PM