在Avalon程序中如何实现右键菜单的添加,可以在前端展现中添加,也可以在程序中动态添加,如下例将实现这两种方式。
<Canvas
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ContextMenus.Pane1"
Name="cv1"
>
<Grid Name="grid" ShowGridLines ="false" Background ="White" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"/>
<ColumnDefinition Width="250"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Margin="10, 10, 0, 0" Grid.Column="0" Grid.Row="0" FontSize="24">Context Menus
</TextBlock>
<TextBlock Margin="10, 10, 0, 0" Grid.Column="0" Grid.Row="1" FontSize="14" Grid.ColumnSpan="2">
The following context menu examples demonstrate:
</TextBlock>
<DockPanel Margin="10, 10, 0, 0" Grid.Column="0" Grid.Row="2">
<Button Name="cmButton" Height="30">Button with Context Menu
<Button.ContextMenu>
<ContextMenu Name="cm" Opened="OnOpened" Closed="OnClosed" 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>
</DockPanel>
<TextBlock Margin="10, 10, 5, 5" Grid.Column="1" Grid.Row="2" FontSize="12"
TextWrapping="WrapWithOverflow" VerticalAlignment="Center">
Simple context menu with submenu items. Also illustrates the Opened
and Closed events.
</TextBlock>
<DockPanel Margin="10, 10, 0, 0" Grid.Column="0" Grid.Row="3">
<Button Height="30">With ToolTip
<Button.ContextMenu>
<ContextMenu HorizontalOffset="50" VerticalOffset="50" HasDropShadow="false">
<MenuItem Header="Edit">
<MenuItem Header="Cut"/>
<MenuItem Header="Copy"/>
<MenuItem Header="Paste"/>
<Separator/>
<MenuItem Header="Context Menu item with ToolTip">
<MenuItem.ToolTip>
<ToolTip>
Some information.
</ToolTip>
</MenuItem.ToolTip>
</MenuItem>
</MenuItem>
</ContextMenu>
</Button.ContextMenu>
</Button>
</DockPanel>
<TextBlock Margin="10, 10, 5, 5" Grid.Column="1" Grid.Row="3" FontSize="12"
VerticalAlignment="Center" TextWrapping="WrapWithOverflow">Context menu
item with tooltip. The offset properties are set which causes the menu
to appear outside the button control.</TextBlock>
<DockPanel Margin="10, 10, 0, 0" Grid.Column="0" Grid.Row="4">
<Button Height="30">Choose a Pet
<Button.ContextMenu>
<ContextMenu Placement="Bottom" PlacementRectangle="30,50,0,0">
<MenuItem Header="Animals">
<MenuItem>
<MenuItem.Header>
<Image Source="data/cat.png"/>
</MenuItem.Header>
</MenuItem>
<MenuItem>
<MenuItem.Header>
<Image Source="data/dog.png"/>
</MenuItem.Header>
</MenuItem>
<MenuItem>
<MenuItem.Header>
<Image Source="data/fish.png"/>
</MenuItem.Header>
</MenuItem>
</MenuItem>
</ContextMenu>
</Button.ContextMenu>
</Button>
</DockPanel>
<TextBlock Margin="10, 10, 5, 5" Grid.Column="1" Grid.Row="4" FontSize="12"
VerticalAlignment="Center" TextWrapping="WrapWithOverflow">
Image used as a context menu item.
</TextBlock>
<DockPanel Margin="10, 10, 0, 0" Grid.Column="0" Grid.Row="5">
<Button Height="30" Content="Disabled Button" IsEnabled="False"
ContextMenuService.ShowOnDisabled="True">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Item 1"/>
<MenuItem Header="Item 2"/>
<MenuItem Header="Item 3"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
</DockPanel>
<TextBlock Margin="10, 10, 5, 5" Grid.Column="1" Grid.Row="5" FontSize="12"
VerticalAlignment="Center" TextWrapping="WrapWithOverflow">Using context menu
service to display a context menu on a disabled button.
</TextBlock>
<Button Height="30" Margin="10, 10, 0, 0" Grid.Column="0" Grid.Row="6" Click="OnClick">
Left click me to create a context menu with C# code.</Button>
<TextBlock Margin="10, 10, 5, 5" Grid.Column="1" Grid.Row="6" FontSize="12"
VerticalAlignment="Center" TextWrapping="WrapWithOverflow">Creating a context menu with C#.
</TextBlock>
</Grid>
<Canvas Name="cv2" Width="550" Height="50" Canvas.Top="305" Canvas.Left="10"></Canvas>
</Canvas>
后台代码动态创建:
<Canvas
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ContextMenus.Pane1"
Name="cv1"
>
<Grid Name="grid" ShowGridLines ="false" Background ="White" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"/>
<ColumnDefinition Width="250"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Margin="10, 10, 0, 0" Grid.Column="0" Grid.Row="0" FontSize="24">Context Menus
</TextBlock>
<TextBlock Margin="10, 10, 0, 0" Grid.Column="0" Grid.Row="1" FontSize="14" Grid.ColumnSpan="2">
The following context menu examples demonstrate:
</TextBlock>
<DockPanel Margin="10, 10, 0, 0" Grid.Column="0" Grid.Row="2">
<Button Name="cmButton" Height="30">Button with Context Menu
<Button.ContextMenu>
<ContextMenu Name="cm" Opened="OnOpened" Closed="OnClosed" 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>
</DockPanel>
<TextBlock Margin="10, 10, 5, 5" Grid.Column="1" Grid.Row="2" FontSize="12"
TextWrapping="WrapWithOverflow" VerticalAlignment="Center">
Simple context menu with submenu items. Also illustrates the Opened
and Closed events.
</TextBlock>
<DockPanel Margin="10, 10, 0, 0" Grid.Column="0" Grid.Row="3">
<Button Height="30">With ToolTip
<Button.ContextMenu>
<ContextMenu HorizontalOffset="50" VerticalOffset="50" HasDropShadow="false">
<MenuItem Header="Edit">
<MenuItem Header="Cut"/>
<MenuItem Header="Copy"/>
<MenuItem Header="Paste"/>
<Separator/>
<MenuItem Header="Context Menu item with ToolTip">
<MenuItem.ToolTip>
<ToolTip>
Some information.
</ToolTip>
</MenuItem.ToolTip>
</MenuItem>
</MenuItem>
</ContextMenu>
</Button.ContextMenu>
</Button>
</DockPanel>
<TextBlock Margin="10, 10, 5, 5" Grid.Column="1" Grid.Row="3" FontSize="12"
VerticalAlignment="Center" TextWrapping="WrapWithOverflow">Context menu
item with tooltip. The offset properties are set which causes the menu
to appear outside the button control.</TextBlock>
<DockPanel Margin="10, 10, 0, 0" Grid.Column="0" Grid.Row="4">
<Button Height="30">Choose a Pet
<Button.ContextMenu>
<ContextMenu Placement="Bottom" PlacementRectangle="30,50,0,0">
<MenuItem Header="Animals">
<MenuItem>
<MenuItem.Header>
<Image Source="data/cat.png"/>
</MenuItem.Header>
</MenuItem>
<MenuItem>
<MenuItem.Header>
<Image Source="data/dog.png"/>
</MenuItem.Header>
</MenuItem>
<MenuItem>
<MenuItem.Header>
<Image Source="data/fish.png"/>
</MenuItem.Header>
</MenuItem>
</MenuItem>
</ContextMenu>
</Button.ContextMenu>
</Button>
</DockPanel>
<TextBlock Margin="10, 10, 5, 5" Grid.Column="1" Grid.Row="4" FontSize="12"
VerticalAlignment="Center" TextWrapping="WrapWithOverflow">
Image used as a context menu item.
</TextBlock>
<DockPanel Margin="10, 10, 0, 0" Grid.Column="0" Grid.Row="5">
<Button Height="30" Content="Disabled Button" IsEnabled="False"
ContextMenuService.ShowOnDisabled="True">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Item 1"/>
<MenuItem Header="Item 2"/>
<MenuItem Header="Item 3"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
</DockPanel>
<TextBlock Margin="10, 10, 5, 5" Grid.Column="1" Grid.Row="5" FontSize="12"
VerticalAlignment="Center" TextWrapping="WrapWithOverflow">Using context menu
service to display a context menu on a disabled button.
</TextBlock>
<Button Height="30" Margin="10, 10, 0, 0" Grid.Column="0" Grid.Row="6" Click="OnClick">
Left click me to create a context menu with C# code.</Button>
<TextBlock Margin="10, 10, 5, 5" Grid.Column="1" Grid.Row="6" FontSize="12"
VerticalAlignment="Center" TextWrapping="WrapWithOverflow">Creating a context menu with C#.
</TextBlock>
</Grid>
<Canvas Name="cv2" Width="550" Height="50" Canvas.Top="305" Canvas.Left="10"></Canvas>
</Canvas>
后台代码动态创建:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Media;
namespace ContextMenus
{
/// <summary>
/// Interaction logic for Pane1.xaml
/// </summary>
public partial class Pane1 : Canvas
{
System.Windows.Controls.ContextMenu contextmenu;
System.Windows.Controls.MenuItem mi, mia, mib, mib1, mib1a;
System.Windows.Controls.Button btn;
void OnOpened(object sender, RoutedEventArgs e)
{
//<SnippetContextMenuIsOpen>
if(cm.IsOpen==true)
{
cmButton.Content = "ContextMenu opened and the IsOpen property is true.";
}
//</SnippetContextMenuIsOpen>
else
{
cmButton.Content = "ContextMenu Opened";
}
}
void OnClosed(object sender, RoutedEventArgs e)
{
cmButton.Content = "ContextMenu Closed";
}
void OnClick(object sender, RoutedEventArgs e)
{
//<Snippet2>
btn = new Button();
btn.Content = "Created with C#";
contextmenu = new ContextMenu();
btn.ContextMenu = contextmenu;
mi = new MenuItem();
mi.Header = "File";
mia = new MenuItem();
mia.Header = "New";
mi.Items.Add(mia);
mib = new MenuItem();
mib.Header = "Open";
mi.Items.Add(mib);
mib1 = new MenuItem();
mib1.Header = "Recently Opened";
mib.Items.Add(mib1);
mib1a = new MenuItem();
mib1a.Header = "Text.xaml";
mib1.Items.Add(mib1a);
contextmenu.Items.Add(mi);
cv2.Children.Add(btn);
//</Snippet2>
}
}
}
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Media;
namespace ContextMenus
{
/// <summary>
/// Interaction logic for Pane1.xaml
/// </summary>
public partial class Pane1 : Canvas
{
System.Windows.Controls.ContextMenu contextmenu;
System.Windows.Controls.MenuItem mi, mia, mib, mib1, mib1a;
System.Windows.Controls.Button btn;
void OnOpened(object sender, RoutedEventArgs e)
{
//<SnippetContextMenuIsOpen>
if(cm.IsOpen==true)
{
cmButton.Content = "ContextMenu opened and the IsOpen property is true.";
}
//</SnippetContextMenuIsOpen>
else
{
cmButton.Content = "ContextMenu Opened";
}
}
void OnClosed(object sender, RoutedEventArgs e)
{
cmButton.Content = "ContextMenu Closed";
}
void OnClick(object sender, RoutedEventArgs e)
{
//<Snippet2>
btn = new Button();
btn.Content = "Created with C#";
contextmenu = new ContextMenu();
btn.ContextMenu = contextmenu;
mi = new MenuItem();
mi.Header = "File";
mia = new MenuItem();
mia.Header = "New";
mi.Items.Add(mia);
mib = new MenuItem();
mib.Header = "Open";
mi.Items.Add(mib);
mib1 = new MenuItem();
mib1.Header = "Recently Opened";
mib.Items.Add(mib1);
mib1a = new MenuItem();
mib1a.Header = "Text.xaml";
mib1.Items.Add(mib1a);
contextmenu.Items.Add(mi);
cv2.Children.Add(btn);
//</Snippet2>
}
}
}