• 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;
                    }
                }
            }
        }
    复制代码

      最后说明一点吧,这次封装对于我自己来说还有一个不满意的地方,那就是选择一些项目以后,界面上不显示已经选择的项,希望有人能够完善一下,给出改造后的代码。

  • 相关阅读:
    hand_git
    面试经典150题——Day17
    MySQL添加、查看、修改与删除数据
    gateway动态网关/通用网关配置
    uniapp 轮播列表左右滑动,滑动到中间放大
    电机学 基础概念 野火电机第四章 电机分类
    计算机组成原理(4)-----Cache的原理及相关知识点
    【方案】基于5G智慧工业园区解决方案(PPT原件)
    【牛客网刷题系列 之 Verilog快速入门】~ 多功能数据处理器、求两个数的差值、使用generate…for语句简化代码、使用子模块实现三输入数的大小比较
    java计算机毕业设计居家养老系统MyBatis+系统+LW文档+源码+调试部署
  • 原文地址:https://www.cnblogs.com/zhenzaizai/p/18120526