How to Get Mouse Position on Context Menu Command(MVVM)

2 posts, 0 answers
  1. Semih
    Semih avatar
    8 posts
    Member since:
    Aug 2014

    Posted 16 Dec 2014 Link to this post


    I'm using MVVM pattern on Diagram. I have a context menu set inside a style for targettype RadDiagramContainerShape. I want to pass the mouse position to the command as parameter (inside "NewShapeMenuItem" MenuItem).

    How can I achieve this?

            <Style TargetType="telerik:RadDiagramContainerShape">            
                <Setter Property="ContextMenu">
                        <ContextMenu Name="ComponentContextMenu">
                            <MenuItem Name="NewMenuItem" Header="New">
                                <MenuItem Name="NewShapeMenuItem" Header="Shape" Command="{Binding AddCommand}" CommandParameter="{Binding ElementName=StateDiagram, Path=ActualHeight}" Icon="{StaticResource GoToShapeImage}"/>
                            <MenuItem Header="Go to State" ItemsSource="{Binding ParentFacility.SelectedComponentViewModel.StateViewModels}"><!--IsEnabled="{Binding ParentFacility.Parent.IsOnline, Mode=TwoWay}"-->
                                    <MultiBinding Converter="{StaticResource ComponentToIsEnabledConverter}" Mode="TwoWay">
                                        <Binding Path="ParentFacility.Parent.IsOnline" Mode="TwoWay"/>
                                        <Binding Path="ParentFacility.SelectedComponentViewModel"/>
                                    <Style TargetType="MenuItem">
                                        <Setter Property="Icon" Value="{StaticResource GoToStateImage}"></Setter>
                                        <Setter Property="Header" Value="{Binding Name}"></Setter>
                                        <Setter Property="Command" Value="{Binding ParentComponent.ParentFacility.GoToStateCommand}"></Setter>
                                        <Setter Property="CommandParameter" Value="{Binding}"></Setter>

  2. Pavel R. Pavlov
    Pavel R. Pavlov avatar
    1237 posts

    Posted 18 Dec 2014 Link to this post

    Hi Semih,

    When working with ContextMenu you need to keep in mind that it actually is in different visual tree. This is why the DataContexts are also different and there is no easy way to gain access to the container which the user has clicked (in order to calculate the mouse position). 

    You can take a look at our documentation for more information.

    Pavel R. Pavlov

    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