Alright, I got a Window in WPF in which I want to set a buttons position in an image.
The image shows a piece of Hardware (An
electronic pipette [
^]) for which I am developing the hardware simulation (The pipette is connected to a medical instrument, and the simulator is intended to let developers of the instrument test the connection and the communication protocol they use to control the pipette (Yes, the Pipette has an RS232 connection).
Whatsoever, I now want to add Buttons onto the image in order to give the user a better idea which button he clicks. Whatever, I tried a solution with margins:
<Grid Grid.Column="0" VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Row="0">Pipette Keys</Label>
<Image Name="pipMenuImage" Grid.Row="1" MouseDown="Image_MouseDown" SizeChanged="pipMenuImage_SizeChanged" Source="pic.jpg"></Image>
<Button x:Name="pipKeyButton" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Bottom" Click="pipKeyButton_Click" Margin="0,0,87,61" Height="25" Width="54">
<TextBlock><Run Text="Pip Key"/></TextBlock>
</Button>
</Grid>
The problem is that the margins are static, and the button does not stay at the position it needs to stay on the image (It slips off the button shown on the image, ending up somewhere in the image).
I also thought about catching the Click-Event of the image and then calculate where the cursor is, and define ranges for the buttons (And then, on every click event, I calculate whether the cursor is located on the background of the image). This possibility falls out, since the user must see that he clicked a button, and therefore I'd have to add something like an overlay to the image, showing the user what he just clicked.
The best way would be to have something like WinForm anchors, but they got a kick up the bum in WPF and are not to be found, despite being used. So here comes the question: Any other idea how I possibly could keep a buttons relative position on an image in WPF?