Context Menus

Jul 22, 2009 at 3:48 PM
Edited Jul 22, 2009 at 3: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.

    <UserControl.Resources>
        <Style TargetType="{x:Type graphsharp:VertexControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type graphsharp:VertexControl}">
                        <Border Width="150" Background="White" 
							BorderBrush="{TemplateBinding BorderBrush}" 
							BorderThickness="{TemplateBinding BorderThickness}"
							CornerRadius="10,10,10,10"
                            DataContext="{TemplateBinding Vertex}"
                            MouseLeftButtonDown="VertexMouseClick"
                            MouseRightButtonDown="VertexRightClick">
                            <ContentPresenter Content="{Binding}" ContentTemplateSelector="{StaticResource SelectVertexTemplate}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="graphsharp:GraphElementBehaviour.HighlightTrigger" Value="{Binding Path=IsMouseOver, RelativeSource={RelativeSource Self}}"/>
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="BorderThickness" Value="2"/>
            <Style.Triggers>
                <Trigger Property="graphsharp:GraphElementBehaviour.IsHighlighted" Value="True">
                    <Setter Property="BorderBrush" Value="Green" />
                </Trigger>
                <Trigger Property="graphsharp:GraphElementBehaviour.IsSemiHighlighted" Value="True">
                    <Setter Property="BorderBrush" Value="Green"/>
                </Trigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="graphsharp:GraphElementBehaviour.IsSemiHighlighted" Value="True"/>
                        <Condition Property="graphsharp:GraphElementBehaviour.SemiHighlightInfo" Value="Source"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="BorderBrush" Value="Red"/>
                </MultiTrigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="graphsharp:GraphElementBehaviour.IsSemiHighlighted" Value="True"/>
                        <Condition Property="graphsharp:GraphElementBehaviour.SemiHighlightInfo" Value="Target"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="BorderBrush" Value="Blue"/>
                </MultiTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>
    
    <Grid Name="gdGraph">
        <zoom:ZoomControl Name="zctGraph"> <!--ContextMenu="{StaticResource cMenu}"-->
            <Test:EntityGraphLayout x:Name="OrganizationalChart"
                                    Graph="{Binding ElementName=OrgChartWindow,Path=GraphToVisualize}"
                                    AnimationLength="0"
                                    LayoutAlgorithmType="Tree"
                                    OverlapRemovalAlgorithmType="FSA"
                                    HighlightAlgorithmType="Simple"/>
        </zoom:ZoomControl>
    </Grid>

Developer
Jul 23, 2009 at 8: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

sorry
badang


 

 

Jul 29, 2009 at 8: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!!