在 VB WPF 中未调用鼠标事件,z 顺序重要吗? [英] Mouse events not called in VB WPF, does z-order matter?
问题描述
google里有很多解释,但是我还是在努力实现鼠标事件.
There are many explanation in google, but I still struggling to implement mouse events.
- 我尝试在画布属性中使用
MouseEnter="Canvas1_MouseEnter"
,但它不起作用. - 我尝试手动添加,.它不起作用.
- 我也尝试在
Private Sub main_Loaded(sender As Object, e As RoutedEventArgs) Handles main.Loaded
方法中使用AddHandler Canvas1.MouseEnter, AddressOf Canvas1_MouseEnter
方法,它没有也不行. - 然后我尝试将
AddHandler Canvas1.MouseEnter, AddressOf Canvas1_MouseEnter
移动到MainWindow.g.i.vb
文件中的Public Sub InitializeComponent()
.它没有用
- I have tried to use
MouseEnter="Canvas1_MouseEnter"
in canvas properties, but it doesn't work. - I tried to add manually, . it doesn't work.
- I also tried to use
AddHandler Canvas1.MouseEnter, AddressOf Canvas1_MouseEnter
inPrivate Sub main_Loaded(sender As Object, e As RoutedEventArgs) Handles main.Loaded
method, it doesn't work too. - Then I tried to move
AddHandler Canvas1.MouseEnter, AddressOf Canvas1_MouseEnter
toPublic Sub InitializeComponent()
inMainWindow.g.i.vb
file. it didn't work
我不知道如何实现鼠标事件.:(
I have no idea how to implement mouse events. :(
所以我尝试将 MouseEnter
实现到它自己的表单中,正确调用事件.
so I tried to implement MouseEnter
to the form it self, the event called properly.
这是否意味着 z 顺序很重要?
Does it mean z-order matter?
这里是xaml文件
<Window x:Name="main" x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="620.242" Width="827.423">
<Grid HorizontalAlignment="Left" Height="575" Margin="10,10,0,0" VerticalAlignment="Top" Width="807">
<Canvas x:Name="Canvas1" HorizontalAlignment="Left" Height="555" Margin="794,368,-774,-348" VerticalAlignment="Top" Width="787" />
</Grid>
</Window>
和处理程序子
'this code generated automatically
Private Sub Canvas1_MouseEnter(sender As Object, e As MouseEventArgs)
MessageBox.Show("event called")
End Sub
如何在画布上实现鼠标事件?任何帮助表示赞赏:)
How could I implement mouse event to my canvas? Any help appreciated :)
推荐答案
WPF 中的 UI 元素不会在其呈现区域之外接收鼠标事件(没有鼠标捕获).除非您为其 Background
属性定义非空值,否则面板(如您的画布)不会呈现其子元素区域之外的任何内容.
UI elements in WPF do not receive mouse events (without mouse capture) outside their rendered area. A Panel (like your Canvas) does not render anything outside the area of its child elements unless you define a non-null value for its Background
property.
你可以为你的 Canvas 设置一个 Background
如下所示,这使得它使用透明画笔渲染由 ActualWidth 和 ActualHeight 定义的整个区域:
You may set a Background
for your Canvas like shown below, which makes it render the entire area defined by its ActualWidth and ActualHeight with a transparent brush:
<Canvas x:Name="Canvas1" Background="Transparent"
MouseEnter="Canvas1_MouseEnter" ... />
这篇关于在 VB WPF 中未调用鼠标事件,z 顺序重要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!