MVVM approach to get MousePosition

4 posts, 0 answers
  1. Etienne
    Etienne avatar
    47 posts
    Member since:
    Sep 2014

    Posted 13 Oct 2014 Link to this post

    Hi,

    I'm trying to get the position of a context menu relative to his placement target by using MousePosition property and there is an issue.

    Here's what I have :

    <telerik:RadContextMenu>
        <telerik:RadMenuItem Header="Add item" Command="{Binging AddItemCommand}" CommandParameter="{Binding MousePosition, RelativeSource={RelativeSource FindAncestor, AncestorType=telerik:RadContextMenu}}"/>
    </telerik:RadContextMenu/>

    When I click on "Add Item", the position that I received in the viewModel is (Double.NaN, Double.Nan), so I conclued that the value of MousePosition is updated after the evaluation of the bindings. Is there another way to get the MousePosition of the contextMenu that would works with MVVM ?

    Thank you,

    Etienne
  2. Kalin
    Admin
    Kalin avatar
    1209 posts

    Posted 14 Oct 2014 Link to this post

    Hello Etienne,

    What I can suggest you would be to try using a binding to the Menu property of the MenuItem, that holds the reference to the ContextMenu. Please test the following binding and let me if it helps:

    <telerik:RadMenuItem Header="Add item" Command="{Binding AddItemCommand}" CommandParameter="{Binding Path=Menu.MousePosition, RelativeSource={RelativeSource Self}}"/>

    Hope this will work for you.

    Regards,
    Kalin
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Etienne
    Etienne avatar
    47 posts
    Member since:
    Sep 2014

    Posted 14 Oct 2014 in reply to Kalin Link to this post

    Hi Kalin,

    I tried your solution, but it doesn't work more than once. The second time I use the context menu to add an item, the item is added at the same place than the first. MousePosition doesn't seem to be updated.

    Thank you,

    Etienne
  5. Kalin
    Admin
    Kalin avatar
    1209 posts

    Posted 15 Oct 2014 Link to this post

    Hi Etienne,

    Indeed the property doesn't updated as it is a regular property. However if you pass the ContextMenu and get the property in the command method you will have the correct data each time:

    <telerik:RadMenuItem Header="Add item"
                            Command="{Binding AddItemCommand}"
                            CommandParameter="{Binding Path=Menu, RelativeSource={RelativeSource Self}}"/>

    And the method of the command in the ViewModel:

    private void OnCommandExecuted(object obj)
    {
        var mousePosition = (obj as RadContextMenu).MousePosition;
    }

    Hope this helps.

    Regards,
    Kalin
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top