How do I MVVM-ize this MouseDown code in my WPF 3D app?
Posted
by DanM
on Stack Overflow
See other posts from Stack Overflow
or by DanM
Published on 2010-05-26T20:37:12Z
Indexed on
2010/05/26
20:41 UTC
Read the original article
Hit count: 791
In my view, I have:
<UserControl x:Class ... MouseDown="UserControl_MouseDown">
<Viewport3D Name="Viewport" Grid.Column="0">
...
</Viewport3D >
</UserControl>
In my code-behind, I have:
private void UserControl_MouseDown(object sender, MouseButtonEventArgs e)
{
((MapPanelViewModel)DataContext).OnMouseDown(e, Viewport);
}
And in my view-model, I have:
public void OnMouseDown(MouseEventArgs e, Viewport3D viewport)
{
var range = new LineRange();
var isValid = ViewportInfo.Point2DtoPoint3D(viewport, e.GetPosition(viewport), out range);
if (!isValid)
MouseCoordinates = "(no data)";
else
{
var point3D = range.PointFromZ(0);
var point = ViewportInfo.Point3DtoPoint2D(viewport, point3D);
MouseCoordinates = e.GetPosition(viewport).ToString() + "\n" + point3D + "\n" + point;
}
}
I really don't have a good sense of how to handle mouse events with MVVM. I always just end up putting them in the code-behind and casting the DataContext
as SomeViewModel
, then passing the MouseEventArgs
on to a handler in my view-model. That's bad enough already, but in this case, I'm actually passing in a control (a Viewport3D
), which is necessary for translating coordinates between 2D and 3D.
Any suggestions on how to make this more in tune with MVVM?
© Stack Overflow or respective owner