MisterPresident
MisterPresident

Reputation: 561

Get MousePosition in a Panel (when Mouse is over an other UIElement) to ViewModel

Again I have a probably simple problem:

I am trying to get the MousePosition in my Scrollviewer:

CurrentMousePosition = e.GetPosition((IInputElement)e.OriginalSource);

But the problem occures when my mouse is over an other UIElement - the source is the UIElement and not my Scrollviewer - "wrong" Position:

I would now that I could do a "IsHitTestVisible="false"" but I also need the HitTest of the Elements.

XAML:

<ScrollViewer>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseMove">
            <cmd:EventToCommand
                Command="{Binding Mode=OneWay, Path=MouseMoveCommand}"                   
                PassEventArgsToCommand="True"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Canvas>
        <ItemsControl ItemsSource="{Binding ItemsToDraw}">
            <!-- A lot of stuff -->
        </ItemsControl>
    </Canvas>
</ScrollViewer>

Thank you!

Upvotes: 1

Views: 88

Answers (1)

denis morozov
denis morozov

Reputation: 6316

I think that before you do your assignment:

CurrentMousePosition = e.GetPosition((IInputElement)e.OriginalSource);

you could do a check and quick exit when the sender is of a different type:

var scollViewer = e.OriginalSource as ScrollViewer;
if(scrollViewer == null) return; //short circuit 
CurrentMousePosition = e.GetPosition(scrollViewer);

Also (with apologies for nagging), but it sounds like your property CurrentMousePosition is really for monitoring the ScrollViewer's mouse position. If that is so, for clarity, it'd be better to name it ScrollViewerMousePosition

Upvotes: 1

Related Questions