Edit 2: Thank you all for your feedback. I solved the problem by adding this to my SelectedDatesChanged event:
Mouse.Capture(null);
When I select a
I handled SelectedDatesChanged event and I was displaying property OriginalSource of MouseButtonEventArgs. When you pick a date it displays Path, Rectangle and so on but when you pick for instance button or whatever beyond calendar it displays precisely System.Windows.Controls.Primitives.CalendarItem. Apparently calendar needs one more click in order to transfer mouse to another UIelement. My idea was to invoke event after first click on calendar so that it could lost capture right away.
public static class CalendarM
{
private static Button tempButton;
public static bool GetRemoveProperty(DependencyObject obj)
{
return (bool)obj.GetValue(RemoveFocusProperty);
}
public static void SetRemoveProperty(DependencyObject obj, bool value)
{
obj.SetValue(RemoveFocusProperty, value);
}
public static readonly DependencyProperty RemoveFocusProperty = DependencyProperty.RegisterAttached("RemoveFocus", typeof(bool), typeof(CalendarM),
new FrameworkPropertyMetadata(new PropertyChangedCallback((x, y) =>
{
if (x is Calendar && GetRemoveProperty((DependencyObject)x))
{
tempButton = new Button() { Width = 0, Height = 0 };
((System.Windows.Controls.Panel)((FrameworkElement)x).Parent).Children.Add(tempButton);
tempButton.Click += (s1, s2) =>
{
};
((Calendar)x).SelectedDatesChanged += CalendarM_SelectedDatesChanged;
}
})));
static void CalendarM_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
{
tempButton.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) { RoutedEvent = Button.MouseDownEvent });
}
}
I created UIelement(in this case button) to invoke its MouseDown event. I had to add it to Panel(setting visibility did not work), otherwise event does not allow to invoke itself. Now when you click calendar it invokes tempButton.Click, it looses capture and when you press your Button "GO" it does not require clicking two times. I know it is a dirty way out, but working.