OnMouseMove 没有被调用

OnMouseMove not getting called

我怀疑问题出在 XAML 但由于某种原因,OnMouseMove 从未被调用。这是 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>

这是我的 C# 代码(它在我几年前编写的另一个程序中运行良好):

 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;
}

无论如何,我确定永远不会触发 OnMouseMove。我想要做的是在图像上跟踪光标(并且,最终,当光标在图像上时将其更改为十字)。就是这样。

如我所说,我怀疑问题出在XAML。也许是绑定? 提前致谢。我确定这是愚蠢的事情。

您需要将 OnMouseMoveMouseMove 事件绑定。在 XAML 中,您可以像设置 属性.

那样做同样的事情
<Image MouseMove="OnMouseMove" ... />