Search code examples
c#wpfxamlonmousemove

OnMouseMove not getting called


I suspect that the problem is with the XAML but, for some reason, OnMouseMove never gets called. Here's the XAML:

<Window x:Class="General_Staff_AI_Testbed.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="General Staff AI Testbed" Height="990" Width="1440" WindowState="Maximized"   Icon="/General Staff AI Testbed;component/Icon1.ico" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" WindowStartupLocation="CenterOwner" >


    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_File">
                <MenuItem Header="_Open Topographical Map..." x:Name="OpenMapFile" Click="OpenMapFile_click" />
                <MenuItem Header="_Open Elevation Map..." x:Name="OpenElevationFile" Click="OpenElevationFile_click" />
            </MenuItem>
            <MenuItem Header="_About">
                <MenuItem Header="_About..."   Click="About_click"/>
            </MenuItem>
        </Menu>

        <StatusBar DockPanel.Dock="Bottom">
            <TextBlock Name="StatusBarField1">Location = X,Y</TextBlock>
            <Separator/>
            <TextBlock Name="StatusBarField2">Elevation = X</TextBlock>
            <Separator/>
            <TextBlock Name="StatusBarField3">Terrain = None</TextBlock>
            <Separator/>
            <TextBlock Name="StatusBarField4">Time 0:00</TextBlock>
            <Separator/>
        </StatusBar>



        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1000" />
                <ColumnDefinition Width="105" />
                <ColumnDefinition Width="105" />
                <ColumnDefinition Width="105" />
                <ColumnDefinition Width="105" />
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="50" />
                <RowDefinition Height="50" />
                <RowDefinition Height="50" />
                <RowDefinition Height="50" />
                <RowDefinition Height="*" />
                <RowDefinition Height="100" />
            </Grid.RowDefinitions>


            <Image Grid.Column="0"   Grid.RowSpan="5"  Height="700" Name="MainImage" Width="1000" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            <Button Grid.Column="1"  Click="Grid_Click" Margin="6,0,0,13">Grid On/Off</Button>

        </Grid>
    </DockPanel>

And this is my C# code (which works fine in another program that I wrote a couple of years ago):

 void OnMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
 {

     string slug;
     double pixelMousePositionX = 0;
     double pixelMousePositionY = 0;

     // Get the x and y coordinates of the mouse pointer.
     System.Windows.Point position = e.GetPosition(this);

     slug = "Location = " + (int)pixelMousePositionX + "," + (int)pixelMousePositionY;
     StatusBarField1.Text = slug;

     if (this.MainImage.IsMouseOver && position.X > 22 && position.Y > 21)
         Mouse.OverrideCursor = System.Windows.Input.Cursors.Cross;
     else
         Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
}

Anyway, I've determined that OnMouseMove never gets triggered. What I'm trying to do is track the cursor over the image (and, eventually, change the cursor to a cross when it's over the image). That's it.

As I said, I suspect that the problem is in the XAML. A binding maybe? Thanks in advance. I'm sure it's something stupid.


Solution

  • You need to bind OnMouseMove with MouseMove event. In XAML you can do that same like setting a property.

    <Image MouseMove="OnMouseMove" ... />