DoubleClick 在 DataGrid 和 DataTemplate 中均触发

长话短说; 我有一个网格内有一个网格。两个网格都有双击事件,这些事件应该触发不同的方法调用(主网格显示一个窗口,而 DataTemplate 中的网格显示一个带有来自所选详细信息行的参数的窗口)。


问题是,即使 e.Handled 设置为 true,双击详细信息行也会调用双击主网格。


简化的 XAML:


<Window x:Class="DoubleClickDataTemplate.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:DoubleClickDataTemplate"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Window.Resources>

        <DataTemplate x:Key="LinesGrid">

            <DataGrid x:Name="dgLines" 

                        ItemsSource="{Binding Path=Lines}" 

                        AutoGenerateColumns="True" 

                        IsReadOnly="True"

                        MouseDoubleClick="dgLines_MouseDoubleClick">

            </DataGrid>

        </DataTemplate>

    </Window.Resources>

    <Grid>

        <DataGrid x:Name="dgFiles" 

                    ItemsSource="{Binding}" 

                    AutoGenerateColumns="True" 

                    IsReadOnly="True"

                    RowDetailsVisibilityMode="VisibleWhenSelected"

                    RowDetailsTemplate="{StaticResource LinesGrid}" 

                    MouseDoubleClick="dgFiles_MouseDoubleClick">

        </DataGrid>

    </Grid>

</Window>

输出显示,当我双击 DataTemplate/DetailRow 时,这两个事件都会被调用:


00:05.616 (00:03:456) dgLines_MouseDoubleClick(object sender, MouseButtonEventArgs e)

                      dgFiles_MouseDoubleClick(object sender, MouseButtonEventArgs e)

最接近“解决方案”的是使用锁定标志(https://www.oipapio.com/question-3430969),但这可能会在很多方面出错。


有没有办法让双击详细信息行只调用相关事件而不是两个事件?


慕妹3146593
浏览 50回答 1
1回答

慕工程0101907

您可以处理MouseLeftButtonDown并检查ClickCount外部DataGrid:private void dgFiles_MouseDoubleClick(object sender, MouseButtonEventArgs e){&nbsp; &nbsp; if (e.ClickCount == 2)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.Diagnostics.Debug.WriteLine("dgFiles_MouseDoubleClick(object sender, MouseButtonEventArgs e)");&nbsp; &nbsp; }}private void dgLines_MouseDoubleClick(object sender, MouseButtonEventArgs e){&nbsp; &nbsp; System.Diagnostics.Debug.WriteLine("dgLines_MouseDoubleClick(object sender, MouseButtonEventArgs e)");&nbsp; &nbsp; e.Handled = true;}XAML:<Window.Resources>&nbsp; &nbsp; <DataTemplate x:Key="LinesGrid">&nbsp; &nbsp; &nbsp; &nbsp; <DataGrid x:Name="dgLines"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ItemsSource="{Binding Path=Lines}"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AutoGenerateColumns="True"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsReadOnly="True"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MouseDoubleClick="dgLines_MouseDoubleClick">&nbsp; &nbsp; &nbsp; &nbsp; </DataGrid>&nbsp; &nbsp; </DataTemplate></Window.Resources><Grid>&nbsp; &nbsp; <DataGrid x:Name="dgFiles"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ItemsSource="{Binding}"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AutoGenerateColumns="True"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsReadOnly="True"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RowDetailsVisibilityMode="VisibleWhenSelected"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RowDetailsTemplate="{StaticResource LinesGrid}"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MouseLeftButtonDown="dgFiles_MouseDoubleClick">&nbsp; &nbsp; </DataGrid></Grid>
打开App,查看更多内容
随时随地看视频慕课网APP