单击顶点的clontext菜单删除顶点

时间:2014-05-10 08:44:51

标签: wpf graph-sharp

我已将上下文菜单添加到顶点按照以下代码

             <Style TargetType="{x:Type graphsharp:VertexControl}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate  TargetType="{x:Type graphsharp:VertexControl}">
                                <Border Background="{TemplateBinding Background}" 
                            BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}"
                            CornerRadius="10,10,10,10"
                            Padding="{TemplateBinding Padding}">
                                    <Border.ContextMenu>
                                        <ContextMenu Opened="ContextMenu_Opened">
                                            <MenuItem x:Name="miDelete" Click="miAdd_Click" Header="Delete" />
                                        </ContextMenu>
                                    </Border.ContextMenu>
                                    <ContentPresenter Content="{TemplateBinding Vertex}" />
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>

通过

向代码页添加了事件
    Public Sub miAdd_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub
    Public Sub ContextMenu_Opened(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

但是如何才能获得顶点的id或值来删除它

1 个答案:

答案 0 :(得分:-1)

C#中的示例 - 我认为重写为vb将没有问题......

<Style TargetType="{x:Type GraphSharp_Controls:VertexControl}">
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu>
                <ContextMenu.ItemContainerStyle>
                    <Style TargetType="MenuItem">
                        <EventSetter Event="Click" Handler="v_ContextMenuClick"/>
                    </Style>
                </ContextMenu.ItemContainerStyle>
                <MenuItem Header="Delete this vertex" CommandParameter="{x:Static SampleProject:ContextCommands.REMOVE}">
                    <MenuItem.Icon>
                        <Image Source="/del.png" />
                    </MenuItem.Icon>
                </MenuItem>
                ...
            </ContextMenu>
        </Setter.Value>
    </Setter>
    <!-- where 'ContextCommands' is just my pre-defined enums... -->
    ...

这是点击处理程序

private void v_ContextMenuClick(object sender, RoutedEventArgs e){
MenuItem mi = (MenuItem)sender;
VertexControl vc = null;
if (mi != null){
    vc = (VertexControl)((ContextMenu)mi.Parent).PlacementTarget;
    //my own class inherited from GraphSharp.Controls.VertexControl
    //you can use what you have directly
    PocVertex pv = (PocVertex)vc.Vertex;
    //same thing with graph class
    //graph is stored globally
    PocGraph gg = App.vm.Graph;

    switch ((ContextCommands)mi.CommandParameter){
        case ContextCommands.REMOVE:
            gg.RemoveVertex(pv);
            break;
        ...
    }
}
e.Handled = true;

}

fyi - 处理函数必须存储在xaml.cs代码下才能看到。 希望这会有所帮助...