【问题标题】:WPF DataGrid MouseLeftButtonUp get fired when maximizing window by double click on title通过双击标题最大化窗口时,WPF DataGrid MouseLeftButtonUp 被触发
【发布时间】:2019-01-09 02:30:21
【问题描述】:

当我通过双击标题栏最大化窗口时,我的 DataGrid 得到扩展,并且无意中触发了 MouseLeftButtonUp 事件。

我使用 MouseLeftButtonUp 来检测单击了哪个单元格。在这种情况下,我想最大化窗口而不是单击 DataGrid 中的元素。

当用户最大化窗口或将其与普通用户单击 DataGrid 区分开来时,如何防止它触发?在这种情况下,ClickCount 等于 1。

这是我的代码

MainWindow.xaml:

<Window x:Class="testing.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:testing"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid MouseLeftButtonUp="MouseSingleClick">
        </DataGrid>
    </Grid>
</Window>

MainWindow.xaml.cs:

private void MouseSingleClick(object sender, MouseButtonEventArgs e)
{
    Console.WriteLine($"Click: {e.ClickCount}");
}

谢谢。

【问题讨论】:

    标签: wpf wpfdatagrid


    【解决方案1】:

    尝试使用 MouseLeftButtonDown 事件而不是 up 事件

    <Window x:Class="testing.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:testing"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid Name="grdDB" MouseLeftButtonDown="grdDB_MouseLeftButtonDown">
        </DataGrid>
    </Grid>
    

    MainWindow.xaml.cs

        private void grdDB_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show($"Click: {e.ClickCount}");
        }
    

    【讨论】:

    • 在我将数据填充到 DataGrid 后,MouseLeftButtonDown 以某种方式停止触发。我必须更改为 PreviewMouseLeftButtonDown 并且它现在可以工作。谢谢。
    猜你喜欢
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 2012-01-09
    • 2015-01-24
    • 1970-01-01
    相关资源
    最近更新 更多