积极答复者
=== wpf双击事件 ====

问题
-
大家好,我现在定义了一个这样类似的控件,想要响应用户双击事件:
<UserControl x:Class="WpfApplication1.fileItem" 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="100" d:DesignWidth="100" Width="100" Height="100" Margin="10,0,0,0" Loaded="UserControl_Loaded" Background="IndianRed" MouseDoubleClick="UserControl_MouseDoubleClick"> <Grid Height="62" Width="81" Background="#FF38A943" MouseUp="Grid_MouseUp"> <Image Height="48" Margin="26,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Source="/WpfApplication1;component/Images/picasabackground.bmp" /> <TextBlock Height="50" HorizontalAlignment="Left" Margin="12,50,0,0" Name="textBlock1" Text="a.rmvb" VerticalAlignment="Top" Width="76" TextAlignment="Center" TextTrimming="CharacterEllipsis" TextWrapping="WrapWithOverflow" /> </Grid> </UserControl>
问题是除了UserControl之外,其他的都不支持DoubleClick事件,但是我的Grid总会挡住UserControl使其接收不到用户UserControl事件,我想问的是,如何解决这个问题。
我看过网上所谓模拟双击地方法,但我不相信wpf连这个玩意都需要模拟。。。
另外我的双击事件其实并不关心是哪个具体控件产生的,我只需要知道我这自定义控件某处被双击了就好了。
多谢大家帮忙了
答案
-
您好 死亡的飞翔
您的 Grid 之所以会档掉你的 MouseDoubleClick 事件 是因为你在你的 Grid 里面处理了 MouseUp 事件,这样系统就会认为你的 MouseDoubleClick 事件已经处理完了。 这个是因为WPF的路由事件机制(关于路由事件的知识 你可以参考http://msdn.microsoft.com/zh-cn/magazine/cc785480.aspx),这里我简单的给你举一个例子 解释一下WPF的工作原理:
加入你现在窗口上控件的叠放顺序是(由下到上):Button -> Grid -> Image
那么当你鼠标左键按下的时候 系统会接受到这样的命令:
Image -> PreviewMouseDown
Grid -> PreviewMouseDown
Button -> PreviewMouseDown
Image -> MouseDown
Grid -> MouseDown
Button -> MouseDown
Button -> PreviewMouseUp
Grid -> PreviewMouseUp
Image -> PreviewMouseUp
Button -> MouseUp
Grid -> MouseUp
Image -> MouseUp
然后再执行各个控件的Click事件, 如果在路由的过程中 有一个事件处理完成(比如e.handled = true), 那么系统会认为路由完成,后面的事件就不会再处理了。
你的程序在执行Click之前, 你的Grid 已经把事件handle完成了。所以你的双击事件才不会触发。
致,
Sheldon _Xiao
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!- 已建议为答案 Jarrey 2010年11月12日 1:25
- 已标记为答案 Sheldon _XiaoModerator 2010年11月15日 9:35
全部回复
-
可以尝试 UserControl 的 "PreviewMouseDoubleClick".
- 已建议为答案 Sheldon _XiaoModerator 2010年11月15日 9:35
-
您好 死亡的飞翔
您的 Grid 之所以会档掉你的 MouseDoubleClick 事件 是因为你在你的 Grid 里面处理了 MouseUp 事件,这样系统就会认为你的 MouseDoubleClick 事件已经处理完了。 这个是因为WPF的路由事件机制(关于路由事件的知识 你可以参考http://msdn.microsoft.com/zh-cn/magazine/cc785480.aspx),这里我简单的给你举一个例子 解释一下WPF的工作原理:
加入你现在窗口上控件的叠放顺序是(由下到上):Button -> Grid -> Image
那么当你鼠标左键按下的时候 系统会接受到这样的命令:
Image -> PreviewMouseDown
Grid -> PreviewMouseDown
Button -> PreviewMouseDown
Image -> MouseDown
Grid -> MouseDown
Button -> MouseDown
Button -> PreviewMouseUp
Grid -> PreviewMouseUp
Image -> PreviewMouseUp
Button -> MouseUp
Grid -> MouseUp
Image -> MouseUp
然后再执行各个控件的Click事件, 如果在路由的过程中 有一个事件处理完成(比如e.handled = true), 那么系统会认为路由完成,后面的事件就不会再处理了。
你的程序在执行Click之前, 你的Grid 已经把事件handle完成了。所以你的双击事件才不会触发。
致,
Sheldon _Xiao
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!- 已建议为答案 Jarrey 2010年11月12日 1:25
- 已标记为答案 Sheldon _XiaoModerator 2010年11月15日 9:35
-
ContentControl有MouseDoubleClick事件。把它Grid作为它的Content就行了<UserControl x:Class="WpfApplication1.fileItem" 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="100" d:DesignWidth="100" Width="100" Height="100" Margin="10,0,0,0" Loaded="UserControl_Loaded" Background="IndianRed" > <ContentControl MouseDoubleClick="ContentControl_MouseDoubleClick"> <Grid Height="62" Width="81" Background="#FF38A943"> <Image Height="48" Margin="26,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Source="/WpfApplication1;component/Images/picasabackground.bmp" /> <TextBlock Height="50" HorizontalAlignment="Left" Margin="12,50,0,0" Name="textBlock1" Text="a.rmvb" VerticalAlignment="Top" Width="76" TextAlignment="Center" TextTrimming="CharacterEllipsis" TextWrapping="WrapWithOverflow" /> </Grid> </ContentControl> </UserControl>
- 已建议为答案 Sheldon _XiaoModerator 2010年11月15日 9:35