WPF 日历控件按住鼠标

发布于 2024-11-07 23:49:33 字数 487 浏览 0 评论 0原文

因此,我在 VS2010 中的全新 WPF 应用程序中删除了 MainWindow.xaml 上的标准 WPF Calendar 控件。如果我单击日历中的某一天,然后尝试单击应用程序的“关闭”按钮,则我必须在关闭按钮上单击两次才能接受单击。它的表现就好像Calendar 尚未释放鼠标来与应用程序的其余部分进行交互。

我已将 Focusable 更改为 false,但没有任何更改,并且我尝试覆盖 PreviewOnMouseUp 并将 ReleaseMouseCapture() 调用为 no有用。我对 MouseLeaveMouseLeftButtonUp 做了同样的事情,得到了相同的结果。鉴于这些事情都不起作用,我怀疑我找错了对象。谷歌没有发现任何值得注意的东西,尽管也许我的 GoogleFu 今天还没有达到标准。

有什么想法吗?

So I dropped the standard WPF Calendar control on the MainWindow.xaml in a brand new WPF App in VS2010. 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.

I've changed Focusable to false, with no change in effect, and I've tried overriding the PreviewOnMouseUp and calling ReleaseMouseCapture() to no avail. I've done the same thing with MouseLeave and MouseLeftButtonUp with the same result. 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.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

御守 2024-11-14 23:49:33

您可以通过使用如下处理程序订阅日历的 PreviewMouseUp 事件来更改此行为:

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

You can change this behavior by subscribing to the calendar's PreviewMouseUp event with a handler like this:

private void Calendar_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    if (Mouse.Captured is CalendarItem)
    {
        Mouse.Capture(null);
    }
}
↘紸啶 2024-11-14 23:49:33

日历控件托管在弹出窗口中,并捕获鼠标。当您第一次单击其他位置时,捕获会将单击发送到弹出窗口,弹出窗口意识到您在其自身外部单击后会关闭。因此,单击不会转到该按钮。

使用 ComboBox 时可以看到相同的效果。将其放下,然后单击按钮。它不会点击按钮。

不幸的是,您不可能采取任何措施来改变这种行为。

编辑:.NET 的最新版本使解决方案成为可能。请参阅艾伦的回答。

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. 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. See Eren's answer.

比忠 2024-11-14 23:49:33

这是我用来解决鼠标捕获问题和子控件缺少 Click 事件的代码的基础。它可能可以进一步简化,以使日历控件更直接地访问,但我个人倾向于将其添加到 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>

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. It can probably be simplified further to make the calendar control more directly accessible, but I personally tend to add it into the 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>
如痴如狂 2024-11-14 23:49:33

这段代码一定有帮助

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

This code must help

Calendar.PreviewMouseUp += (o, e) =>
{
    if (!e.OriginalSource.Equals(Calendar))
    {
        Mouse.Capture(null);
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文