WPF 上位机自定义控件系列菜单 WxMenu右键菜单 WxContextMenu 主要是设计 MenuItem 的样式,还挺复杂的,要考虑不同层级的 Header、Item,考虑 IsChecked 功能 ![](data:image/svg+xml;utf8,svg%20xmlns='http://www.w3.org/2000/svg'%20width='768'%20height='422'/svg) 参考了 HandyControl 里的样式,并增加了 Iconfont 属性来简化 Icon 的添加过程 ![](data:image/svg+xml;utf8,svg%20xmlns='http://www.w3.org/2000/svg'%20width='991'%20height='202'/svg) WxMenuItem 控件using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WpfControlsX.ControlX
{
public class WxMenuItem : MenuItem
{
static WxMenuItem()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(WxMenuItem), new FrameworkPropertyMetadata(typeof(WxMenuItem)));
}
///
/// 图标
///
public Geometry Iconfont
{
get => (Geometry)GetValue(IconfontProperty);
set => SetValue(IconfontProperty, value);
}
public static readonly DependencyProperty IconfontProperty =
DependencyProperty.Register("Iconfont", typeof(Geometry), typeof(WxMenuItem), new PropertyMetadata(null));
///
/// 图标尺寸
///
public double IconSize
{
get => (double)GetValue(IconSizeProperty);
set => SetValue(IconSizeProperty, value);
}
public static readonly DependencyProperty IconSizeProperty =
DependencyProperty.Register("IconSize", typeof(double), typeof(WxMenuItem), new PropertyMetadata(10d));
///
/// Header 宽度
///
public double HeaderWidth
{
get => (double)GetValue(HeaderWidthProperty);
set => SetValue(HeaderWidthProperty, value);
}
public static readonly DependencyProperty HeaderWidthProperty =
DependencyProperty.Register("HeaderWidth", typeof(double), typeof(WxMenuItem), new PropertyMetadata(24d));
}
}
WxMenuItem、WxMenu、WxContextMenu 样式
使用
|