Mouse events not called in VB WPF, does z-order matter?

后端 未结 2 1148
野的像风
野的像风 2021-01-26 09:14

There are many explanation in google, but I still struggling to implement mouse events.

  • I have tried to use MouseEnter=\"Canvas1_MouseEnter\" in canva
相关标签:
2条回答
  • 2021-01-26 09:41

    Your canvas does not have the MouseEnter event. Use it like this

    <Grid>
        <Canvas HorizontalAlignment="Left" Height="158" Margin="191,85,0,0" VerticalAlignment="Top" 
                Width="221" Background="#FF575757" MouseEnter="Canvas_MouseEnter"/>
    </Grid>
    

    And then the Sub for the Event:

    Private Sub Canvas_MouseEnter(sender As Object, e As MouseEventArgs)
        MessageBox.Show("Hello?")
    End Sub
    
    0 讨论(0)
  • 2021-01-26 09:48

    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.

    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" ... />
    
    0 讨论(0)
提交回复
热议问题