WPF show MouseMove position











up vote
0
down vote

favorite












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.










share|improve this question




















  • 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 at 14:57












  • You might need to calculate the possition relative to the screen.
    – ikerbera
    Nov 21 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 at 15:00






  • 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 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 <Button Content ="Test" /> it will behave exactly the same.
    – Bodul
    Nov 21 at 15:08

















up vote
0
down vote

favorite












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.










share|improve this question




















  • 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 at 14:57












  • You might need to calculate the possition relative to the screen.
    – ikerbera
    Nov 21 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 at 15:00






  • 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 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 <Button Content ="Test" /> it will behave exactly the same.
    – Bodul
    Nov 21 at 15:08















up vote
0
down vote

favorite









up vote
0
down vote

favorite











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.










share|improve this question















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.







c# wpf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 15:00









Lews Therin

2,27811436




2,27811436










asked Nov 21 at 14:53









Bodul

898




898








  • 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 at 14:57












  • You might need to calculate the possition relative to the screen.
    – ikerbera
    Nov 21 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 at 15:00






  • 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 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 <Button Content ="Test" /> it will behave exactly the same.
    – Bodul
    Nov 21 at 15:08
















  • 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 at 14:57












  • You might need to calculate the possition relative to the screen.
    – ikerbera
    Nov 21 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 at 15:00






  • 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 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 <Button Content ="Test" /> it will behave exactly the same.
    – Bodul
    Nov 21 at 15:08










2




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 at 14:57






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 at 14:57














You might need to calculate the possition relative to the screen.
– ikerbera
Nov 21 at 14:59




You might need to calculate the possition relative to the screen.
– ikerbera
Nov 21 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 at 15:00




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 at 15:00




1




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 at 15:05






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 at 15:05






1




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 <Button Content ="Test" /> it will behave exactly the same.
– Bodul
Nov 21 at 15:08






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 <Button Content ="Test" /> it will behave exactly the same.
– Bodul
Nov 21 at 15:08














1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










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






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414719%2fwpf-show-mousemove-position%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    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






    share|improve this answer



























      up vote
      2
      down vote



      accepted










      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






      share|improve this answer

























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        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






        share|improve this answer














        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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 at 15:22

























        answered Nov 21 at 15:11









        tetralobita

        384513




        384513






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414719%2fwpf-show-mousemove-position%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...