一、Xaml中增加contextmenu与menuitem的click事件:
<Image Height="484" Name="image1" Stretch="Fill" Width="900">
<Image.ContextMenu>
<ContextMenu Name="cm" StaysOpen="true">
<MenuItem Header="File"/>
<MenuItem Header="Save" Name="MISave" Click="MenuItem_Click"/>
<!--<MenuItem Header="SaveAs"/>-->
<!--<MenuItem Header="Recent Files">
<MenuItem Header="ReadMe.txt"/>
<MenuItem Header="Schedule.xls"/>
</MenuItem>-->
</ContextMenu >
</Image.ContextMenu>
</Image>private void Window_Loaded(object sender, RoutedEventArgs e)//LOAD时加载click事件
{
SaveImage()
dgSeData.AutoGenerateColumns = true
getscnstr()
}
private void MenuItem_Click(object sender, RoutedEventArgs e)//事件定义
{
MessageBox.Show("GoGoGo!")
}
private void SaveImage()//调用事件
{
MISave.Click += MenuItem_Click
} 本文来自子成的博客
ContextMenu类就是用来做右键菜单的对象,对于任何的控件都可以进行对ContextMenu属性的 *** 作进行设置右键菜单的功能.下面代码就是对一个按钮添加一个WPF右键菜单的功能:
<Button Name="cmButton"
Height="30">Button with
Context Menu
<Button.ContextMenu>
<ContextMenu Name="cm"
StaysOpen="true">
<MenuItem Header="File"/>
<MenuItem Header="Save"/>
<MenuItem Header="SaveAs"/>
<MenuItem Header="Recent Files">
<MenuItem Header="ReadMe.txt"/>
<MenuItem Header="Schedule.xls"/>
</MenuItem>
</ContextMenu>
</Button.ContextMenu>
</Button>
包括任何控件,只要有ContextMenu属性都可以添加WPF右键菜单.
<Grid><ListView x:Name="lvw"
MouseDoubleClick="ListView_MouseDoubleClick">
<ListView.Items>
<ListViewItem>item1</ListViewItem>
<ListViewItem>item2</ListViewItem>
</ListView.Items>
</ListView>
</Grid> private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
object o = lvw.SelectedItem
if (o == null)
return
ListViewItem item = o as ListViewItem
MessageBox.Show(item.Content.ToString())
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)