Context Menus

Jul 22, 2009 at 2:48 PM
Edited Jul 22, 2009 at 2:52 PM

I've been attempting to get a context menu working and am having difficulty and was wondering if anyone had any example code?  The issue is that the highlight is removed when the context menu pops up and I'm having trouble finding the vertex that was right clicked.  Should I just use a right click event or use the regular context menu property?  The other issue is I need to have the context menu be vertex dependent.  I've got about 5 different types of verticies and the context menu will be different based on the vertex clicked.  I also need one to work when right clicking the background.

Sorry if this seems stupid, I'm very new to WPF in general.  I've got a GraphLayout called EntityGraphLayout that works on a EntityGraph containing Entity objects.  I have children types of Entity that I actually use to populate my graphs.  The type of each object is what is needed to build the context menu/choose the appropriate context menu.  Templates for the objects are choosen using a ContentTemplateSelector.

Any help you can offer would be appreciated.

        <Style TargetType="{x:Type graphsharp:VertexControl}">
            <Setter Property="Template">
                    <ControlTemplate TargetType="{x:Type graphsharp:VertexControl}">
                        <Border Width="150" Background="White" 
							BorderBrush="{TemplateBinding BorderBrush}" 
							BorderThickness="{TemplateBinding BorderThickness}"
                            DataContext="{TemplateBinding Vertex}"
                            <ContentPresenter Content="{Binding}" ContentTemplateSelector="{StaticResource SelectVertexTemplate}"/>
            <Setter Property="graphsharp:GraphElementBehaviour.HighlightTrigger" Value="{Binding Path=IsMouseOver, RelativeSource={RelativeSource Self}}"/>
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="BorderThickness" Value="2"/>
                <Trigger Property="graphsharp:GraphElementBehaviour.IsHighlighted" Value="True">
                    <Setter Property="BorderBrush" Value="Green" />
                <Trigger Property="graphsharp:GraphElementBehaviour.IsSemiHighlighted" Value="True">
                    <Setter Property="BorderBrush" Value="Green"/>
                        <Condition Property="graphsharp:GraphElementBehaviour.IsSemiHighlighted" Value="True"/>
                        <Condition Property="graphsharp:GraphElementBehaviour.SemiHighlightInfo" Value="Source"/>
                    <Setter Property="BorderBrush" Value="Red"/>
                        <Condition Property="graphsharp:GraphElementBehaviour.IsSemiHighlighted" Value="True"/>
                        <Condition Property="graphsharp:GraphElementBehaviour.SemiHighlightInfo" Value="Target"/>
                    <Setter Property="BorderBrush" Value="Blue"/>
    <Grid Name="gdGraph">
        <zoom:ZoomControl Name="zctGraph"> <!--ContextMenu="{StaticResource cMenu}"-->
            <Test:EntityGraphLayout x:Name="OrganizationalChart"
                                    Graph="{Binding ElementName=OrgChartWindow,Path=GraphToVisualize}"

Jul 23, 2009 at 7:26 AM

Very fast reply:

you can define the entity vertex clicked by accessing the DataContext property of the border (in your case). Try something like this:


        private void vertexTextBlock_MouseDown(object sender, MouseButtonEventArgs e)
            VertexClicked = sender as Border;
            EntityVertex ev = VertexClicked.DataContext as EntityVertex;
The you can create your custom ContextMenu based on the vertex clicked int the code behind....

Now I have to go :(

Follow us and wait for a complete reply




Jul 29, 2009 at 7:02 PM

I've added a MouseEnter and MouseLeave event to the Border for VertexControl to get/remove the Entity and hold in a private variable on the Window.  The thing I was wondering is how do I make it so the highlight is linked to this variable instead of the IsMouseOver?  That should fix the Highlight disappearing when the ContextMenu opens.  Is there a way to tell Graph# "highlight this vertex" and "stop highlighting this vertex" specifically?

Thanks for the help!!