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();
}