在XAML中,指定控件的ContentMenu属性下,添加一个具体的<ContentMenu>标签即可,在运行时,点击鼠标右键,就会弹出这个菜单。
以TreeView为例
<TreeView Name="treeView"> <TreeViewItem Header="演示弹出菜单"/> <!--渐变背景--> <TreeView.Background> <LinearGradientBrush EndPoint="0.63,0.483" StartPoint="-0.295,0.483"> <GradientStop Color="#FF8A8383" Offset="0"/> <GradientStop Color="#FFFFFFFF" Offset="1"/> </LinearGradientBrush> </TreeView.Background> <!-- 此处就实现了一个弹出菜单的制作--> <TreeView.ContextMenu> <ContextMenu Name="popMenu" StaysOpen="True"> <MenuItem Name="refleshTree" Header="刷新" Click="refleshTree_Click"></MenuItem> </ContextMenu> </TreeView.ContextMenu> </TreeView>