0
votes

When I maximize the window by double click the title bar, my DataGrid get expanded and the MouseLeftButtonUp event is fired unintentionally.

I used MouseLeftButtonUp to detect which cell has been clicked. In this case, I want to maximize the window instead of clicking element inside DataGrid.

How can I prevent it from firing when user is maximizing the window or distinguish it from normal user click on DataGrid? ClickCount equals to 1 in this case.

Here is my code

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}");
}

Thanks.

1

1 Answers

0
votes

Try use MouseLeftButtonDown event instead of up event

<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}");
    }