C#实现的下拉多选框,下拉多选树,多级节点
今天给大家上个硬货,下拉多选框,同时也是下拉多选树,支持父节点跟子节点!该控件是基于Telerik控件封装实现的,所以大家在使用的过程中需要引用Telerik.WinControls.dll、Telerik.WinControls.UI.dll,还有一些相关的类库,大家有需要的可以去网上自己找,另外我也会把一些动态库放到CSDN上面,大家需要可以去下载。
[ToolboxItem(true)]
public partial class DropDownTreeViewControl : RadControl
{
public DropDownTreeViewElement TreeViewElement { get; private set; } public RadTreeView TreeViewControl
{
get
{
return this.TreeViewElement.TreeViewControl;
}
} protected override void CreateChildItems(RadElement parent)
{
this.AllowShowFocusCues = true; base.CreateChildItems(parent); this.TreeViewElement = new DropDownTreeViewElement(); parent.Children.Add(TreeViewElement); } protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
this.TreeViewElement.Focus();
} protected override void OnBindingContextChanged(EventArgs e)
{
base.OnBindingContextChanged(e); this.TreeViewControl.BindingContext = this.BindingContext;
} public class DropDownTreeViewElement : LightVisualElement
{
private readonly Color BG_COLOR = Color.White;
private readonly Color BORDER_COLOR = Color.LightBlue;
private readonly Color ARROW_BORDER_COLOR = Color.LightGray;
private readonly Color ARROW_NORMAL_BG_COLOR = Color.White;
private readonly Color ARROW_MOUSE_OVER_BG_COLOR = Color.LightYellow;
private readonly Color ARROW_PRESSED_BG_COLOR = Color.DarkOrange; private readonly int NORMAL_BORDER_WIDTH = 1;
private readonly int FOCUS_BORDER_WIDTH = 2; private RadArrowButtonElement arrow;
private PopupForm popup;
private bool isInnerCallHide; public bool IsPopupOpen { get; private set; }
public RadTreeView TreeViewControl
{
get
{
return this.popup.TreeView;
}
} protected override void InitializeFields()
{
base.InitializeFields(); // style
this.DrawBorder = true;
this.BorderBoxStyle = BorderBoxStyle.SingleBorder;
this.BorderGradientStyle = GradientStyles.Solid;
this.BorderColor = BORDER_COLOR;
this.DrawFill = true;
this.NumberOfColors = 1;
this.GradientStyle = GradientStyles.Solid;
this.BackColor = BG_COLOR;
this.StretchHorizontally = true;
this.StretchVertically = true;
} protected override void CreateChildElements()
{
base.CreateChildElements(); // arrow
this.CreateArrow(); // popup
this.CreatePopup(); this.Children.Add(arrow);
} private void CreatePopup()
{
this.popup = new PopupForm(this);
this.popup.PopupClosing += Popup_PopupClosing;
this.popup.PopupClosed += Popup_PopupClosed;
} private void Popup_PopupClosing(object sender, RadPopupClosingEventArgs args)
{
// mouse postion in control-bounds prevent
if (args.CloseReason == RadPopupCloseReason.Mouse)
{
var boundsSc = RectangleToScreen(this.Bounds);
if (boundsSc.Contains(MousePosition))
{
args.Cancel = true;
}
}
} private void Popup_PopupClosed(object sender, RadPopupClosedEventArgs args)
{
if (isInnerCallHide)
{
return;
}
this.IsPopupOpen = false;
this.SwitchArrowState(false);
} private void CreateArrow()
{
this.arrow = new RadArrowButtonElement()
{
ClickMode = ClickMode.Press,
MinSize = new Size(SystemInformation.VerticalScrollBarWidth,
RadArrowButtonElement.RadArrowButtonDefaultSize.Height),
StretchHorizontally = false,
StretchVertically = true,
Margin = new System.Windows.Forms.Padding(2),
}; arrow.Fill.NumberOfColors = 1;
arrow.Fill.BackColor = ARROW_NORMAL_BG_COLOR;
arrow.Border.BoxStyle = BorderBoxStyle.SingleBorder;
arrow.Border.GradientStyle = GradientStyles.Solid;
arrow.Border.ForeColor = ARROW_BORDER_COLOR; arrow.RadPropertyChanged += Arrow_RadPropertyChanged;
arrow.Click += Arrow_Click;
} private void Arrow_Click(object sender, EventArgs e)
{
if (this.IsPopupOpen)
{
this.IsPopupOpen = false;
this.SwitchArrowState(false);
this.HidePopup();
}
else
{
this.IsPopupOpen = true;
this.SwitchArrowState(true);
this.ShowPopup();
}
} private void HidePopup()
{
this.isInnerCallHide = true;
this.popup.Hide();
this.isInnerCallHide = false;
} private void ShowPopup()
{
this.popup.Width = this.Bounds.Width;
this.popup.Height = 250;
this.popup.ShowPopup(this.RectangleToScreen(this.ControlBoundingRectangle));
} private void SwitchArrowState(bool isPressed)
{
this.arrow.Fill.BackColor = isPressed ? ARROW_PRESSED_BG_COLOR :
(arrow.IsMouseOver ? ARROW_MOUSE_OVER_BG_COLOR : ARROW_NORMAL_BG_COLOR);
} protected override void OnPropertyChanged(RadPropertyChangedEventArgs e)
{
if (e.Property == ContainsFocusProperty)
{
var isFocus = (bool)e.NewValue;
this.BorderWidth = isFocus ? FOCUS_BORDER_WIDTH : NORMAL_BORDER_WIDTH;
} base.OnPropertyChanged(e);
} protected override SizeF ArrangeOverride(SizeF finalSize)
{
base.ArrangeOverride(finalSize); // arrow on right side
float width = this.arrow.DesiredSize.Width;
float x = this.RightToLeft ? 0f : (finalSize.Width - width);
RectangleF finalRect = new RectangleF(x, 0f, width, finalSize.Height);
this.arrow.Arrange(finalRect); return finalSize;
} private void Arrow_RadPropertyChanged(object sender, RadPropertyChangedEventArgs e)
{
if (e.Property == RadArrowButtonElement.IsMouseOverProperty)
{
if (this.IsPopupOpen)
{
return;
} var arrow = sender as RadArrowButtonElement;
var isMouseOver = (bool)e.NewValue; arrow.Fill.BackColor = isMouseOver ? ARROW_MOUSE_OVER_BG_COLOR : ARROW_NORMAL_BG_COLOR;
}
} } public class PopupForm : RadSizablePopupControl
{
private HostTreeView tv; public PopupForm(RadItem owner)
: base(owner)
{
this.SizingMode = SizingMode.UpDownAndRightBottom;
this.VerticalAlignmentCorrectionMode = AlignmentCorrectionMode.SnapToOuterEdges;
} public RadTreeView TreeView
{
get
{
return this.tv.TreeView;
}
} protected override void CreateChildItems(RadElement parent)
{
base.CreateChildItems(parent); this.tv = new HostTreeView();
this.tv.TreeView.Focusable = false;
this.tv.TreeView.CheckBoxes = true;
this.SizingGripDockLayout.Children.Add(tv);
} public override bool OnMouseWheel(Control target, int delta)
{
if (delta < 0)
{
this.tv.TreeView.VScrollBar.PerformSmallIncrement(1);
}
else
{
this.tv.TreeView.VScrollBar.PerformSmallDecrement(1);
} return true;
}
} public class HostTreeView : Telerik.WinControls.RadHostItem
{
public HostTreeView()
: base(new RadTreeView())
{ } public RadTreeView TreeView
{
get
{
return this.HostedControl as RadTreeView;
}
}
}
}
最后说明一点吧,这次封装对于我自己来说还有一个不满意的地方,那就是选择一些项目以后,界面上不显示已经选择的项,希望有人能够完善一下,给出改造后的代码。
C#实现的下拉多选框,下拉多选树,多级节点的更多相关文章
- odoo wizard界面显示带复选框列表及勾选数据获取
实践环境 Odoo 14.0-20221212 (Community Edition) 需求描述 如下图(非实际项目界面截图,仅用于介绍本文主题),打开记录详情页(form视图),点击某个按钮(图中的 ...
- js 复选框 全选都选 如果某一个子复选框没选中 则全选按钮不选中
<!DOCTYPE HTML> <html> <head> <meta charset=UTF-8> <title>js 复选框 全选都选 ...
- js 全选选框与取消全选代码
设置一个全选选框和四个子选框,要实现点击全选后四个子选框选中,取消全选后四个子选框也取消.全选后点击某个子选框,全选也能取消.当四个子选框都选中时,全选框也被选择. 实现代码: <script& ...
- ASP.NET MVC中使用MvcPager异步分页+在分页中复选框下一页上一页也保持选中
ASP.NET MVC 分页使用的是作者杨涛的MvcPager分页控件 地址:http://www.webdiyer.com/mvcpager/demos/ajaxpaging/ 这个分页控件在里面 ...
- [oldboy-django][2深入django]老师管理 -- form表单如何生成多选框标签,多选框的默认值显示,以及多选框数据插入到数据库,多选框数据更改到数据库
1 form表单如何生成多选框(包含了多选框可选择内容) - Form设置班级输入框为 select多选 - 多选 class TeacherForm(Form): name = fields.Cha ...
- 实现在DevExpress.XtraGrid.GridControl的列头绘制复选框以实现全选的功能
首先新建一个Win Form测试项目,拖一个GridControl控件到窗体上. public partial class Form1 : Form { public Form1() { Initia ...
- excel添加复选框和去掉复选框
添加复选框 我测试的excel版本是最新版2016,所有版本都是找开发者工具里面包含很多工具呢,大家可以慢慢测试 excel的右上角 点击文件-->选项-->自定义功能区-->添加开 ...
- easyui datagrid 让某行复选框置灰不能选
easyui中datagrid 让某行复选框置灰不能进行选中操作,以下为主要部分的code. //加载完毕后获取所有的checkbox遍历 onLoadSuccess: function(data){ ...
- element-ui 里面el-checkbox多选框,实现全选单选
data里面定义了 data:[], actionids:[],//选择的那个actionid num1:0,//没选择的计数 num2:0,//选中的计数 ...
- jquery中判断复选框有没有被选上
页面部分: <input type="checkbox" id="cbx" /><label for="cbx">点 ...
随机推荐
- 【Azure 事件中心】Event Hub Client 连接超时(OperationTimeout)测试及解说
Azure Event Hub(Azure事件中心) 是大数据流式处理平台和事件引入服务. 它可以每秒接收和处理数百万个事件.在我们的使用中,需要代码编写的是两个部分:事件生产者和事件接收者 事件生成 ...
- CPack 入门指南
背景 CPack 是 CMake 2.4.2 之后的一个内置工具,用于创建软件的二进制包和源代码包. CPack 在整个 CMake 工具链的位置. CPack 支持打包的包格式有以下种类: 7Z ( ...
- 从 Neo4j 导入 Nebula Graph 实践见 SPark 数据导入原理
本文主要讲述如何使用数据导入工具 Nebula Graph Exchange 将数据从 Neo4j 导入到 Nebula Graph Database.在讲述如何实操数据导入之前,我们先来了解下 Ne ...
- Python 潮流周刊第 40 期(摘要)
本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...
- sentinel 持久化
1. 概述 在前面的关于Sentinel的使用中,可以发现,Sentinel-dashboard配置的规则,在我们的微服务以及控制台重启的时候就清空了,因为他是基于内存的. 所以我们有必要将规则配置进 ...
- Java 类的结构之三 :构造器(或构造方法,constructor)的使用
1 /* 2 * 类的结构之三 :构造器(或构造方法,constructor)的使用 3 * construct:建设 建造 4 * 5 * 一.构造器的作用: 6 * 创建对象 7 * 初始化对象的 ...
- TLS原理与实践(二)
主页 个人微信公众号:密码应用技术实战 个人博客园首页:https://www.cnblogs.com/informatics/ 引言 在上一篇博客中,我们通过<一文读懂TLS1.2协议](ht ...
- 封装TornadoFx常用控件库
github:https://github.com/Stars-One/common-controls 为TornadoFx的封装的常用控件与工具,基于Jfoenix,借鉴Kfoenix 前言 这个开 ...
- iot梳理
近段时间一直在搞公司的iot项目,没啥时间学习新的知识(也是自己懒),这边记录下整体对iot知识领域的认识. 首先说到iot会想到物联网,对于我们开发来说物联网很明显要用到几个不太常用到的技术,如mq ...
- leetcode 春季比赛3题 二叉搜索树染色
其实和二叉搜索树一点关系都没有. 每个点的颜色只取决于最后一次包含该点的操作.用 set 维护所有颜色待确定的点,倒序处理询问,每次利用 lower_bound 方法从 set 中取出所有在询问范围内 ...