Search code examples
c#wpfcursor

Setting IsHitTestVisible to false affects the cursor in wpf


I am having one grid. Inside that i am having some controls.

Expected Behavior :

While mouse down occurs with one of the controls inside the grid i need to change my cursor to wait and need to prevent the next hit until particular time, so in the MouseDown event am setting the cursor to wait and IsHitTestVisible of image to false. but this is not working as expected.

Code Block

<Grid x:Name="Grid1" Grid.Row="1" Grid.Column="1" Cursor="{Binding CurrentCursor}">
  <Canvas x:Name="canvasElement" Width="{Binding ElementName=pupilGrid, Path=ActualWidth}" Height="{Binding ElementName=pupilGrid, Path=ActualHeight}" IsManipulationEnabled="True">
    <Image x:Name="pupilSource" Width="{Binding ElementName=pupilGrid, Path=ActualWidth}" Height="{Binding ElementName=pupilGrid, Path=ActualHeight}" Source="{Binding ImageSource}"   MouseDown="Source_MouseDown" IsHitTestVisible="{Binding CanHitAxis}"/>
    <controls:Control x:Name="targetControl" Width="30" Height="30" ClipToBounds="False" Canvas.Left="0" Canvas.Top="0" VerticalAlignment="Center" HorizontalAlignment="Center"/>
  </Canvas>
</Grid>

What I am getting :

in the "Source_mousedown event" I am setting the current cursor to wait and IsHitTestVisible to false. but the wait cursor is not set to the image. if i removed IsHitTestVisible property of image in the sense wait cursor is get applied to the image also. Can any one help me on this?


Solution

  • If you set the IsHitTestVisible property to false for a UIelement, it will no longer respond to mouse input and will not fire mouse-related events. Bescuse with IsHitTestVisible set to false, the mouse could not able to see ur UIElement in the window. So the cursor property of your grid doesn't work.

    You could use the IsEnabled property to prevent your mouse down event for a particular time rather than IsHitTestVisible.

    I think this one will solve your problem.