<UserControl MouseRightButtonDown="UserControl_MouseRightButtonDown" >
<UserControl.ContextMenu >
<ContextMenu Name="menu">
<MenuItem Header="删除" Click="Delete_Click">
</MenuItem>
</ContextMenu>
</UserControl.ContextMenu>
</UserControl>
//在xaml用户控件中加入ContextMenu,并添加“删除”MenuItem
</UserControl>
//在xaml用户控件中加入ContextMenu,并添加“删除”MenuItem
private void UserControl_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
menu.IsOpen = true;
}
//右键点击事件激发ContextMenu
private void Delete_Click(object sender, RoutedEventArgs e)
{
if (this.Tag != null)
{
WrapPanel Wp = VisualTreeHelper.GetParent(this) as WrapPanel;
if (Wp != null)
{
Wp.Children.Remove(this);
}
}
} //利用VisualTreeHelper找到控件的容器Wrappanel,并删除