This is a migrated thread and some comments may be shown as answers.

MVVM approach to get MousePosition

3 Answers 392 Views
ContextMenu
This is a migrated thread and some comments may be shown as answers.
Etienne
Top achievements
Rank 1
Etienne asked on 13 Oct 2014, 05:29 PM
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

3 Answers, 1 is accepted

Sort by
0
Kalin
Telerik team
answered on 14 Oct 2014, 11:00 AM
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.

 
0
Etienne
Top achievements
Rank 1
answered on 14 Oct 2014, 03:41 PM
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
0
Kalin
Telerik team
answered on 15 Oct 2014, 10:36 AM
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.

 
Tags
ContextMenu
Asked by
Etienne
Top achievements
Rank 1
Answers by
Kalin
Telerik team
Etienne
Top achievements
Rank 1
Share this question
or