Wpf 通过单击顶点的克隆文本菜单删除顶点
我通过以下代码向vertex添加了上下文菜单Wpf 通过单击顶点的克隆文本菜单删除顶点,wpf,graph-sharp,Wpf,Graph Sharp,我通过以下代码向vertex添加了上下文菜单 <Style TargetType="{x:Type graphsharp:VertexControl}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate Targ
<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>
但是我怎样才能得到顶点的id或值来删除它呢?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... -->
...
}
仅供参考-处理程序函数必须存储在xaml.cs代码下才能可见。
希望这有助于
<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;