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

    Hi,

    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">
                    <Setter.Value>
                        <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>
                            <Separator/>
                            <MenuItem Header="Go to State" ItemsSource="{Binding ParentFacility.SelectedComponentViewModel.StateViewModels}"><!--IsEnabled="{Binding ParentFacility.Parent.IsOnline, Mode=TwoWay}"-->
                                <MenuItem.IsEnabled>
                                    <MultiBinding Converter="{StaticResource ComponentToIsEnabledConverter}" Mode="TwoWay">
                                        <Binding Path="ParentFacility.Parent.IsOnline" Mode="TwoWay"/>
                                        <Binding Path="ParentFacility.SelectedComponentViewModel"/>
                                    </MultiBinding>
                                </MenuItem.IsEnabled>
                                <MenuItem.ItemContainerStyle>
                                    <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>
                                    </Style>
                                </MenuItem.ItemContainerStyle>
                            </MenuItem>
                        </ContextMenu>
                    </Setter.Value>
                </Setter>
            </Style>

  2. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1182 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.

    Regards,
    Pavel R. Pavlov
    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
Back to Top