MVVM (Caliburn.Micro を使用) で Silverlight を使用しています。次のように、ビューに単純な四角形を作成しました。
<Rectangle x:Name="Rectangle2" cal:Message.Attach="[Event MouseMove] = [Action MouseMoved($eventArgs)]" Fill="#FFE2F5FF" HorizontalAlignment="Left" Height="220" Margin="195,43,0,0" Stroke="Black" VerticalAlignment="Top" Width="195" />
ViewModel には次のものがあります。
public void MouseMoved(MouseEventArgs mouseEventArgs)
{
var point = mouseEventArgs.GetPosition(null);
String str = string.Format("({0}\t{1})",point.X,point.Y);
MVVMCoords = str;
}
GetPosition に null を入れているので、表示される座標は絶対です。長方形に対して相対的に作成するにはどうすればよいですか? (したがって、長方形の左上隅は0,0です)私が理解している限り、これはViewModelがViewのUIElementsを認識していなくても実行できます。
みんなありがとう