简体   繁体   English

按住鼠标的WPF日历控件

[英]WPF Calendar Control holding on to the Mouse

So I dropped the standard WPF Calendar control on the MainWindow.xaml in a brand new WPF App in VS2010. 因此,我在VS2010中全新的WPF应用程序中的MainWindow.xaml上放了标准的WPF Calendar控件。 If I click on a day in the calendar and then try to click the Close button for the app, I have to click twice on the close button before it accepts the click. 如果我单击日历中的某一天,然后尝试单击该应用程序的“关闭”按钮,则在接受该单击之前,我必须在“关闭”按钮上单击两次。 It's acting as if the Calendar hasn't released the Mouse to interact with the rest of the application. 就像Calendar尚未释放鼠标以与应用程序的其余部分进行交互一样。

I've changed Focusable to false, with no change in effect, and I've tried overriding the PreviewOnMouseUp and calling ReleaseMouseCapture() to no avail. 我已经将Focusable更改为false,并且效果没有变化,并且尝试覆盖PreviewOnMouseUp并调用ReleaseMouseCapture()无济于事。 I've done the same thing with MouseLeave and MouseLeftButtonUp with the same result. 我用MouseLeaveMouseLeftButtonUp做同样的事情,结果是一样的。 Given that none of those things are working I suspect I'm barking up the wrong tree. 鉴于所有这些都不起作用,我怀疑我在树错了树。 Google has turned up nothing of note, though perhaps my GoogleFu is not up to snuff today. Google并没有引起任何注意,尽管今天我的GoogleFu可能还没有成功。

Any ideas? 有任何想法吗?

You can change this behavior by subscribing to the calendar's PreviewMouseUp event with a handler like this: 您可以通过使用如下处理程序订阅日历的PreviewMouseUp事件来更改此行为:

private void Calendar_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    if (Mouse.Captured is CalendarItem)
    {
        Mouse.Capture(null);
    }
}

The calendar control is hosted in a popup, and captures the mouse. 日历控件位于一个弹出窗口中,并捕获鼠标。 When you click somewhere else the first time, the capture sends the click to the popup, which, realizing that you've clicked outside of itself, closes. 第一次单击其他位置时,捕获会将点击发送到弹出窗口,该弹出窗口实现了您在自身外部单击的功能。 The click therefore does not go to the button. 因此,单击不会转到该按钮。

You can see the same effect when using a ComboBox. 使用ComboBox时,您可以看到相同的效果。 Drop it down, then click on a button. 将其放下,然后单击一个按钮。 It won't click the button. 它不会单击该按钮。

Unfortunately, it's unlikely you can do anything to alter this behavior. 不幸的是,您不可能采取任何措施来改变这种行为。

Edit : More recent versions of .NET make a solution possible. 编辑 :.NET的最新版本使解决方案成为可能。 See Eren's answer. 请参阅Eren的答案。

This code must help 此代码必须帮助

Calendar.PreviewMouseUp += (o, e) =>
{
    if (!e.OriginalSource.Equals(Calendar))
    {
        Mouse.Capture(null);
    }
};

This is the basis of the code I use to work around both the mouse capture issue and the lack of Click events from child controls. 这是我用来解决鼠标捕获问题和子控件缺少Click事件的代码的基础。 It can probably be simplified further to make the calendar control more directly accessible, but I personally tend to add it into the UserControl. 可以进一步简化它,以便更直接地访问日历控件,但我个人倾向于将其添加到UserControl中。

class FixedCalendar : UserControl
{
    public FixedCalendar()
    {
        InitializeComponent();
    }

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

            var element = e.OriginalSource as FrameworkElement;
            if (element != null)
                element.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
        }
    }
}

<UserControl x:Class="FixedCalendar"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Calendar x:Name="Calendar" />
</UserControl>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM