WPF中创建上下文菜单

界面设计如下:

<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;

        }

    }

}

 

  

运行效果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysh05

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值