今天给大家上个硬货,下拉多选框,同时也是下拉多选树,支持父节点跟子节点!该控件是基于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. 第120篇: DOM编程(常用操作、动态脚本、样式及动态表格)

    好家伙,我回来了, 本篇为<JS高级程序设计>第十四章"DOM编程"学习笔记   1.DOM编程 我们知道DOM是HTML文档的编程接口, 我们可以通过HTML代码实现 ...

  2. 51从零开始用Rust编写nginx,江湖救急,TLS证书快过期了

    wmproxy wmproxy已用Rust实现http/https代理, socks5代理, 反向代理, 负载均衡, 静态文件服务器,websocket代理,四层TCP/UDP转发,内网穿透等,会将实 ...

  3. STL-RBT_map,set模拟实现

    set #include"26RBT_container.h" namespace test { //set通过普通迭代器使用const迭代器实现限制key不能被修改 templa ...

  4. Java 多线程---线程优先级

    Java 实例 - 线程优先级设置 以下实例演示了如何通过setPriority() 方法来设置线程的优先级: 1 SimplePriorities.java 文件 2 public class Si ...

  5. C++ //set/multiset 容器 //set不可以插入重复的数字 multiset可以插入重复的数字 //ste容器构造和赋值 //set大小和交换 //set 插入和删除 //set查找和统计 //set 和 multiset 区别 //pair 对组创建 //set存放自定义数据类型 //set内置数据 进行排序

    1 //set/multiset 容器 //set不可以插入重复的数字 multiset可以插入重复的数字 2 //ste容器构造和赋值 //set大小和交换 //set 插入和删除 3 //set查 ...

  6. 关于Java并发多线程的一点思考

    写在开头 在过去的2023年双11活动中,天猫的累计访问人次达到了8亿,京东超60个品牌销售破10亿,直播观看人数3.0亿人次,订单支付频率1分钟之内可达百万级峰值,这样的瞬间高并发活动,给服务端带来 ...

  7. 【译】32位 .NET Framework 项目的 WinForm 设计器选择

    在客户反馈的推动下,Visual Studio 2022 向64位架构过渡,标志着增强开发体验的关键一步.正如 Klaus Loffelmann 在他的博客文章中所描述的那样,这种转换增强了整体性能和 ...

  8. 单词本z develop vel = 到上面 从下面到上面的一种过程 抽象是相对从无到有

    单词本z develop vel = 到上面 从下面到上面的一种过程 抽象是相对从无到有 develop 发展 开发 de = down 下面 velop 这里 vel 就是 lev的反写 op = ...

  9. 剑桥英英在线词典 - 可以查单词 可数-不可数 - 英语 a few/few/a little/little

    There is _____ milk in a fridge. Let's go buy some. A. a few B. few C. a little D. little 解析:经典老知识点 ...

  10. deepin版的微信无法启动

    我在网上找了半天的了,出现问题的原因应该是deepin-wine的问题: 既然是deepin-wine出了问题,那重装一下就好了,嗯~ 我直接,yay!结果发现有一个deepin-wine需要更新,更 ...