I'm coding a fixed-size map on a canvas that is built from polygon user controls that then has another user control for game counters added on top. As I play around with different user interface options, the first thing I wanted to do was get the Mouse position when the left button is clicked. When the mouse is clicked, regardless of where on the canvas the mouse is I get the same x and y values returned. Also, the canvas is inside a scroll viewer as the Canvas/map is larger than the window, when I move the window, the returned value changes dependent on where the top left hand corner of the windo is, but doesn't chnage if I click different points within the canvas/ I'm obviously missing something with my implimentation. XAML and C# code below, any advice would be appreciated.
XAML:
<DockPanel> <ToolBarTray DockPanel.Dock="Top"> <ToolBar > <Menu> <MenuItem Header="Turn Options"> <MenuItem Header="Advance Phase"/> <MenuItem Header="End Turn" /> <Separator/> </MenuItem> <MenuItem Header="Map Options"> <MenuItem Header="Load Map"/> </MenuItem> <Separator /> <MenuItem Header="Designer Mode"/> <Separator/> <MenuItem Header="Economics"> <MenuItem Header="Open Economics"/> </MenuItem> <Separator/> <MenuItem Header="Game Stats"/> </Menu> </ToolBar> </ToolBarTray> <ScrollViewer DockPanel.Dock ="Top" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" > <Canvas x:Name="MainMap" Background="Black" Height="1740" Width="4740" IsHitTestVisible="true" MouseDown="MainMap_MouseClick" > </Canvas> </ScrollViewer> </DockPanel>
private void MainMap_MouseClick(object sender, MouseButtonEventArgs e) { try { Control src = e.Source as Control; if (e.Source is GameCounter) { if (src != null) { switch (e.ChangedButton) { case MouseButton.Left: GameCounter clickedGameCounter = (GameCounter)e.Source; Point p = Mouse.GetPosition(MainMap ); MessageBox.Show("X = " + p.X + " Y = " + p.Y); break; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }