今天给大家上个硬货,下拉多选框,同时也是下拉多选树,支持父节点跟子节点!该控件是基于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#实现的下拉多选框,下拉多选树,多级节点的更多相关文章

  1. odoo wizard界面显示带复选框列表及勾选数据获取

    实践环境 Odoo 14.0-20221212 (Community Edition) 需求描述 如下图(非实际项目界面截图,仅用于介绍本文主题),打开记录详情页(form视图),点击某个按钮(图中的 ...

  2. js 复选框 全选都选 如果某一个子复选框没选中 则全选按钮不选中

    <!DOCTYPE HTML> <html> <head> <meta charset=UTF-8> <title>js 复选框 全选都选 ...

  3. js 全选选框与取消全选代码

    设置一个全选选框和四个子选框,要实现点击全选后四个子选框选中,取消全选后四个子选框也取消.全选后点击某个子选框,全选也能取消.当四个子选框都选中时,全选框也被选择. 实现代码: <script& ...

  4. ASP.NET MVC中使用MvcPager异步分页+在分页中复选框下一页上一页也保持选中

    ASP.NET MVC 分页使用的是作者杨涛的MvcPager分页控件  地址:http://www.webdiyer.com/mvcpager/demos/ajaxpaging/ 这个分页控件在里面 ...

  5. [oldboy-django][2深入django]老师管理 -- form表单如何生成多选框标签,多选框的默认值显示,以及多选框数据插入到数据库,多选框数据更改到数据库

    1 form表单如何生成多选框(包含了多选框可选择内容) - Form设置班级输入框为 select多选 - 多选 class TeacherForm(Form): name = fields.Cha ...

  6. 实现在DevExpress.XtraGrid.GridControl的列头绘制复选框以实现全选的功能

    首先新建一个Win Form测试项目,拖一个GridControl控件到窗体上. public partial class Form1 : Form { public Form1() { Initia ...

  7. excel添加复选框和去掉复选框

    添加复选框 我测试的excel版本是最新版2016,所有版本都是找开发者工具里面包含很多工具呢,大家可以慢慢测试 excel的右上角 点击文件-->选项-->自定义功能区-->添加开 ...

  8. easyui datagrid 让某行复选框置灰不能选

    easyui中datagrid 让某行复选框置灰不能进行选中操作,以下为主要部分的code. //加载完毕后获取所有的checkbox遍历 onLoadSuccess: function(data){ ...

  9. element-ui 里面el-checkbox多选框,实现全选单选

    data里面定义了 data:[],        actionids:[],//选择的那个actionid        num1:0,//没选择的计数        num2:0,//选中的计数  ...

  10. jquery中判断复选框有没有被选上

    页面部分: <input type="checkbox" id="cbx" /><label for="cbx">点 ...

随机推荐

  1. MIG是如何向DDR中写入数据的

    1.1    先来看看信号线的描述 我们以X16的器件为例,下面的截图来自 镁光的官方手册 https://media-www.micron.com/-/media/client/global/doc ...

  2. 老生常谈的iOS- weak原理,你真的懂得还是为了应付面试

    前言 weak对于iOS开发来说只要解决一些对象相互引用的时候,避免出现强强引用,对象不能被释放,出现内存泄露的问题. weak 关键字的作用域弱引用,所引用对象的计数器不会加一,并在引用对象被释放的 ...

  3. Java 多线程------解决 实现Runnabel接口方式线程的线程安全问题 方式二:同步方法 +总结

    方式二:同步方法* 如果操作共享数据的代码完整的声明在一个方法中,我们不妨将此方法声明同步的 1 package bytezero.threadsynchronization; 2 3 4 5 /** ...

  4. Java UML类图

    在UML的静态机制中类图是一个重点,它不但是设计人员关心的核心,更是实现人员关注的核心.建模工具也主要根据类图来产生代码.类图在UML的9个图中占据了一个相当重要的地位.James Rumbaugh对 ...

  5. 专访容智信息柴亚团:最低调的公司如何炼成最易用的RPA?

    专访容智信息柴亚团:最低调的公司如何炼成最易用的RPA? 专访容智信息柴亚团:终极愿景是助力天下企业成为数字化孪生组织 文/王吉伟 6月,容智信息(容智)正式发布了全新的移动端RPA产品iBot Mo ...

  6. TLS原理与实践(四)国密TLS

    主页 个人微信公众号:密码应用技术实战 个人博客园首页:https://www.cnblogs.com/informatics/ 引言 TLS作为保证网络通信安全的关键技术和基石被广泛应用,但目前主流 ...

  7. linux文件管理(补充)

    linux文件管理 vim编辑器 vi概述 vi 编辑器 他是linux和unix系统上最基本的文本编辑器,类似于windows系统下的记事本编辑器 vim编辑器 vim是vi的加强版,比vi更容易使 ...

  8. Python爬虫实战系列3:今日BBNews编程新闻采集

    一.分析页面 打开今日BBNews网址 https://news.bicido.com ,下拉选择[编程]栏目 1.1.分析请求 F12打开开发者模式,然后点击Network后点击任意一个请求,Ctr ...

  9. linux介绍、安装、shell

    1-Linux发展介绍 零 什么是Linux Linux:和我们常见的Windows一样,都是操作系统,但不同的是: Windows: 收费,不开源,主要用于日常办公.游戏.娱乐多一些. Linux: ...

  10. kali linux安装vmware tools过程详解

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/robacco/article/deta ...