silverlight 右键菜单类

本文介绍了一种在 WPF 应用中实现上下文菜单的方法。通过自定义 `ContextMenu` 类并创建 `RTBContextMenu` 子类,可以轻松地为应用程序添加上下文菜单功能。文章详细解释了如何构造菜单项、显示菜单以及处理点击事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.ContextMenu

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Windows.Controls.Primitives;
using System.ComponentModel;
using System.Windows.Automation.Peers;

namespace Projects_Manager.ViewModels
{
    public abstract class ContextMenu
    {
        private Point _location;
        private bool _isShowing;
        private Popup _popup;
        private Grid _grid;
        private Canvas _canvas;
        private FrameworkElement _content;

        //自定义数据
        private object _vis;
        private int _idex=-1;
        //Intiialize and show the popup window. This is the public method to call to display the ContextMenu at the desired location.
        public void Show(Point location)
        {
            if (_isShowing)
                throw new InvalidOperationException();

            _isShowing = true;
            _location = location;
            ConstructPopup();
            _popup.IsOpen = true;
        }

        //Close the popup window
        public void Close()
        {
            _isShowing = false;

            if (_popup != null)
            {
                _popup.IsOpen = false;
            }
        }
        public bool GetIsShow()
        {
            return _isShowing;
        }
        public void SetObject(object vis)
        {
            _vis = vis;
        }
        public object GetObject()
        {
            return _vis;
        }

        public void SetIdex(int idex)
        {
            _idex = idex;
        }
        public int GetIdex()
        {
            return _idex;
        }

        //abstract function that the child class needs to implement to return the framework element that needs to be displayed in the popup window.
        public abstract FrameworkElement GetContent();
        //Default behavior for OnClickOutside() is to close the context menu when there is a mouse click event outside the context menu
        protected virtual void OnClickOutside()
        {
            Close();
        }

        // Construct a popup window thats the size of the application with a grid layout
        // Add a canvas as a child of the grid layout to detect mouse clicks outside the context menu
        // Add the Framework Element returned by GetContent() to the grid and position it at _location

        private void ConstructPopup()
        {
            if (_popup != null)
                return;

            _popup = new Popup();
            _grid = new Grid();

            _popup.Child = _grid;

            _canvas = new Canvas();

            _canvas.MouseLeftButtonDown += (sender, args) => { OnClickOutside(); };
            _canvas.MouseRightButtonDown += (sender, args) => { args.Handled = true; OnClickOutside(); };

            _canvas.Background = new SolidColorBrush(Colors.Transparent);

            _grid.Children.Add(_canvas);

            _content = GetContent();

            _content.HorizontalAlignment = HorizontalAlignment.Left;
            _content.VerticalAlignment = VerticalAlignment.Top;
            _content.Margin = new Thickness(_location.X, _location.Y, 0, 0);


            _grid.Children.Add(_content);

            UpdateSize();
        }
        private void UpdateSize()
        {
            _grid.Width = Application.Current.Host.Content.ActualWidth;
            _grid.Height = Application.Current.Host.Content.ActualHeight;

            if (_canvas != null)
            {
                _canvas.Width = _grid.Width;
                _canvas.Height = _grid.Height;
            }
        }
    }
} 

2.RTBContextMenu

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;

namespace Projects_Manager.ViewModels
{
    public class RTBContextMenu:ContextMenu
    {

        Border border = new Border() { BorderBrush = new SolidColorBrush(Color.FromArgb(255, 167, 171, 176)), BorderThickness = new Thickness(1), Background = new SolidColorBrush(Colors.White) };
        StackPanel sta = new StackPanel() { Orientation = Orientation.Vertical, HorizontalAlignment = HorizontalAlignment.Center };
        int TabIdex = -1;
         public RTBContextMenu()
        {
            border.Effect = new DropShadowEffect() { BlurRadius = 3, Color = Color.FromArgb(255, 230, 227, 236) };
        }

        //Construct the context menu and return the parent FrameworkElement.

        public override FrameworkElement GetContent()
        {
            border.Child = sta;
            return border;
        }

        public void AddItem(string item)
        {
            StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
            sp.MouseEnter += MouseEnter;
            sp.MouseLeave += MouseLeave;
            TextBlock Text0 = new TextBlock() { Text = item, FontSize=14, HorizontalAlignment = HorizontalAlignment.Center, Margin = new Thickness(10, 5, 10, 5) };
            Text0.Tag = ++TabIdex;
            Text0.MouseLeftButtonUp += BntMenuClick;
            sp.Children.Add(Text0);
            sta.Children.Add(sp);
        }
        public void BntMenuClick(object sender, RoutedEventArgs e)
        {
            TextBlock bnt = (sender as TextBlock);
            SetIdex(int.Parse(bnt.Tag.ToString()));
            Close();
        }

        public void MouseEnter(object sender, RoutedEventArgs e)
        {
            StackPanel bnt = (sender as StackPanel);
            bnt.Background = new SolidColorBrush(Colors.LightGray);
        }
        public void MouseLeave(object sender, RoutedEventArgs e)
        {
            StackPanel bnt = (sender as StackPanel);
            bnt.Background = new SolidColorBrush(Colors.White);
        }
    }
}

3.应用

   public void ProListMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }
        public void ProListMouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            TextBlock tr = (TextBlock)sender;
            RTBContextMenu rt = new RTBContextMenu();
            rt.AddItem("dsdc");
            rt.AddItem("scjkcd");
            Point p = new Point();
            p.X = e.GetPosition(tr).X;
            p.Y = e.GetPosition(tr).Y;
            if (User.Group == 0)
            {
                rt.Show(p);
            }

            Thread th = new Thread(() => ThreadBegin(tr, rt));
            th.Start();
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值