界面设计如下:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ContextMenu x:Key="cmButton">
<MenuItem Header="Menu item 1" />
<MenuItem Header="Menu item 2" />
<Separator />
<MenuItem Header="Menu item 3" />
</ContextMenu>
<ContextMenu x:Key="ContextMenu">
<MenuItem Name="MiFullScreen" Header="全屏"/>
<Separator></Separator>
<MenuItem Name="MiPlay" Header="播放"/>
</ContextMenu>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<Button x:Name="btnSample" Content="Right_Click on Me" FontSize="18" Margin="10" VerticalAlignment="Center" HorizontalAlignment="Left">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Red" x:Name="menuRed" Click="menuRed_Click"/>
<MenuItem Header="Green" x:Name="menuGreen" Click="menuGreen_Click"/>
<MenuItem Header="Blue" x:Name="menuBlue" Click="menuBlue_Click"/>
<Separator/>
<MenuItem Header="Small" x:Name="small" Click="small_Click"></MenuItem>
<MenuItem Header="Normal" x:Name="Normal" Click="Normal_Click"/>
<MenuItem Header="Big" x:Name="Big" Click="Big_Click"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
<TextBox Text="Right-click here for context menu!" Margin="10">
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Command="Cut">
<MenuItem.Icon>
<Image Source="/Images/cut.png" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="Copy">
<MenuItem.Icon>
<Image Source="/Images/copy.png" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="Paste">
<MenuItem.Icon>
<Image Source="/Images/paste.png" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
<Button x:Name="cmButton" Click="cmButton_Click" Margin="10" FontWeight="Bold">FindResource Window_ContextMenu Test</Button>
<Button x:Name="btnMenu" ContextMenu="{StaticResource ContextMenu}" Margin="10">Test</Button>
</StackPanel>
</Grid>
</Window>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void menuRed_Click(object sender, RoutedEventArgs e)
{
this.btnSample.Foreground = new SolidColorBrush(Colors.Red);
}
private void menuGreen_Click(object sender, RoutedEventArgs e)
{
this.btnSample.Foreground = new SolidColorBrush(Colors.Green);
}
private void menuBlue_Click(object sender, RoutedEventArgs e)
{
this.btnSample.Foreground = new SolidColorBrush(Colors.Blue);
}
private void small_Click(object sender, RoutedEventArgs e)
{
this.btnSample.FontSize = 10;
}
private void Normal_Click(object sender, RoutedEventArgs e)
{
this.btnSample.FontSize = 16;
}
private void Big_Click(object sender, RoutedEventArgs e)
{
this.btnSample.FontSize = 26;
}
private void cmButton_Click(object sender, RoutedEventArgs e)
{
ContextMenu cm = this.FindResource("cmButton") as ContextMenu;
cm.PlacementTarget = sender as Button;
cm.IsOpen = true;
}
}
}
运行效果如下:
网友评论