今天给大家上个硬货,下拉多选框,同时也是下拉多选树,支持父节点跟子节点!该控件是基于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. 什么是Redis持久化?

    Redis持久化指的是将内存中的数据同步到硬盘文件,并在redis重新启动的时候将数据备份到硬盘上,从而保证数据的安全性.通过持久化, Redis可以在系统关闭时将数据保存到硬盘上,避免了数据丢失的风 ...

  2. Java --- 多线程 创建线程的方式四: 使用线程池

    1 package bytezero.thread2; 2 3 import java.security.Provider; 4 import java.util.concurrent.Executo ...

  3. 2024-03-09:用go语言,我们把无限数量的栈排成一行,按从左到右的次序从 0 开始编号, 每个栈的的最大容量 capacity 都相同。实现一个叫「餐盘」的类 DinnerPlates, Di

    2024-03-09:用go语言,我们把无限数量的栈排成一行,按从左到右的次序从 0 开始编号, 每个栈的的最大容量 capacity 都相同.实现一个叫「餐盘」的类 DinnerPlates, Di ...

  4. 添加 alt + d 打开 dicts.cn 网址

    代码 autohotkey 代码 限制在双核浏览器 内部使用 #IfWinActive ahk_exe ChromeCore.exe !d:: Run, http://www.dicts.cn/ Re ...

  5. linux文件管理(补充)

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

  6. 基于R语言的raster包读取遥感影像

      本文介绍基于R语言中的raster包,读取单张或批量读取多张栅格图像,并对栅格图像数据加以基本处理的方法. 1 包的安装与导入   首先,我们需要配置好对应的R语言包:前面也提到,我们这里选择基于 ...

  7. Hexo Next主题vercel页面NOT_FOUND

    前端时间将博客部署到了Vercel上,使用的是Hexo Next主题.发现某些博文点进去以后会出现找不到的情况: 404: NOT_FOUND Code: NOT_FOUND ID: ... ... ...

  8. Salesforce LWC学习(四十九) RefreshView API实现标准页面更新,自定义组件自动捕捉更新

    本篇参考: https://developer.salesforce.com/docs/platform/lwc/guide/data-refreshview.html https://develop ...

  9. Cesium之CustomShader

    1. 引言 Cesium自1.87.1版本,开始支持3DTileset使用CustomShader: Added CustomShader class for styling Cesium3DTile ...

  10. 记录--经常被cue大文件上传,忍不住试一下

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 大文件上传主要步骤: 获取文件对象,切分文件 根据文件切片,计算文件唯一hash值 上传文件切片,服务端保存起来 合并文件切片,前端发送合 ...