0

I have XAML:

<Grid MouseMove="onMouseMove" >
  <ItemsControl Name="btnTableImageList">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <Button Content="{Binding Content}" 
                Height="{Binding Height}" 
                Width="{Binding Width}" 
                Tag="{Binding Tag}" 
                Margin="{Binding Margin}" 
                Background="{Binding Background}" 
                HorizontalAlignment="Center"  
                MouseDown="tblButton_MouseDown" 
                MouseUp="tblButton_MouseUp" 
                Click="ClickHandlerTableBtn" 
                TextBlock.TextAlignment="Center" />
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</Grid>

And code behind:

private void onMouseMove(object sender, MouseEventArgs e)
{
    lblCoord.Content = Mouse.GetPosition(Application.Current.MainWindow);
}

On the form there is Label named lblCoord, and there are two buttons that are created after form is loaded.

I want to display mouse coordinate in lblCoord in relation to the Grid, but coords are displayed only when i move mouse cursor over any of the buttons that are placed inside that grid.

My guess is that I am placing MouseMove="onMouseMove" in wrong place.

Thanks for your help.

Lews Therin
  • 3,707
  • 2
  • 27
  • 53
Bodul
  • 185
  • 2
  • 12
  • 2
    Without testing this I know that Button in WPF captures the mouse click, it may also be capturing the MouseMove. I'd have to test and see. If so, in WPF you can use PreviewMouseMove which is captured outmost first. So try using PreviewMouseMove and see if that works. Events are tunneled down from outer most content to inner and raised usually with events prefixed with Preview. Other events are bubbled from inner most to out and do not include the Preview prefix. – Michael Puckett II Nov 21 '18 at 14:57
  • [You might need to calculate the possition relative to the screen.](https://stackoverflow.com/a/19790851/8024781) – ikerbera Nov 21 '18 at 14:59
  • Same thing happens with PreviewMouseMove. position is shown only when mouse cursor goes over buttons and completly ignores grid, or whatever else is behind cursor except if it's a button. – Bodul Nov 21 '18 at 15:00
  • Code works but only when I Mouseover over a button and ignores everything else. I know that I did something wrong, but I don't know what :) – Bodul Nov 21 '18 at 15:02
  • 1
    I'll have to play with this in code. What is the content in the button? Verify that content isn't capturing the events. Just scanning it that's my assumption but I'm not sure. It may be that the Grid doesn't fire the MouseMove event if it's transparent bc I know you can click through transparency in WPF. If it's a transparency issue you can make the Background a color that's so transparent (but not completely) you can't see it but it still registers click. Possibly the same with MouseMove... – Michael Puckett II Nov 21 '18 at 15:05
  • 1
    Buttons are dynamicly created during runtime, so a lot of stuff is loaded from database. But for test you can change XAML button code to it will behave exactly the same. – Bodul Nov 21 '18 at 15:08
  • Ok... then do the transparency test and let me know. Apologies I'm on the phone and can't give you a real answer ATM. I'm just speculating for now. – Michael Puckett II Nov 21 '18 at 15:11
  • I did, and it was transparency as you suggested. Thank you! Please create answer so I can accept it. – Bodul Nov 21 '18 at 15:12
  • Aha, looks like there's an answer below that is stating Background is the culprit. – Michael Puckett II Nov 21 '18 at 15:12
  • Well you answered that 6 minutes ago... It is your answer if you want it. – Bodul Nov 21 '18 at 15:13
  • It's ok. Give it to the answer below but thank you for allowing me the opportunity. – Michael Puckett II Nov 21 '18 at 15:13

1 Answers1

2

It will work when you set Background of Grid to anything but Transparent As default, Grid's background is transparent. When it is transparent, mouse events work when you set Background="Transparent" too.

Mouse events handled nearest parent element with background IMHO

tetralobita
  • 453
  • 6
  • 16