翼度科技»论坛 编程开发 .net 查看内容

C#实现的下拉多选框,下拉多选树,多级节点

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
  今天给大家上个硬货,下拉多选框,同时也是下拉多选树,支持父节点跟子节点!该控件是基于Telerik控件封装实现的,所以大家在使用的过程中需要引用Telerik.WinControls.dll、Telerik.WinControls.UI.dll,还有一些相关的类库,大家有需要的可以去网上自己找,另外我也会把一些动态库放到CSDN上面,大家需要可以去下载。
  1. [ToolboxItem(true)]
  2.     public partial class DropDownTreeViewControl : RadControl
  3.     {
  4.         public DropDownTreeViewElement TreeViewElement { get; private set; }
  5.         public RadTreeView TreeViewControl
  6.         {
  7.             get
  8.             {
  9.                 return this.TreeViewElement.TreeViewControl;
  10.             }
  11.         }
  12.         protected override void CreateChildItems(RadElement parent)
  13.         {
  14.             this.AllowShowFocusCues = true;
  15.             base.CreateChildItems(parent);
  16.             this.TreeViewElement = new DropDownTreeViewElement();
  17.             parent.Children.Add(TreeViewElement);
  18.         }
  19.         protected override void OnEnter(EventArgs e)
  20.         {
  21.             base.OnEnter(e);
  22.             this.TreeViewElement.Focus();
  23.         }
  24.         protected override void OnBindingContextChanged(EventArgs e)
  25.         {
  26.             base.OnBindingContextChanged(e);
  27.             this.TreeViewControl.BindingContext = this.BindingContext;
  28.         }
  29.         public class DropDownTreeViewElement : LightVisualElement
  30.         {
  31.             private readonly Color BG_COLOR = Color.White;
  32.             private readonly Color BORDER_COLOR = Color.LightBlue;
  33.             private readonly Color ARROW_BORDER_COLOR = Color.LightGray;
  34.             private readonly Color ARROW_NORMAL_BG_COLOR = Color.White;
  35.             private readonly Color ARROW_MOUSE_OVER_BG_COLOR = Color.LightYellow;
  36.             private readonly Color ARROW_PRESSED_BG_COLOR = Color.DarkOrange;
  37.             private readonly int NORMAL_BORDER_WIDTH = 1;
  38.             private readonly int FOCUS_BORDER_WIDTH = 2;
  39.             private RadArrowButtonElement arrow;
  40.             private PopupForm popup;
  41.             private bool isInnerCallHide;
  42.             public bool IsPopupOpen { get; private set; }
  43.             public RadTreeView TreeViewControl
  44.             {
  45.                 get
  46.                 {
  47.                     return this.popup.TreeView;
  48.                 }
  49.             }
  50.             protected override void InitializeFields()
  51.             {
  52.                 base.InitializeFields();
  53.                 // style
  54.                 this.DrawBorder = true;
  55.                 this.BorderBoxStyle = BorderBoxStyle.SingleBorder;
  56.                 this.BorderGradientStyle = GradientStyles.Solid;
  57.                 this.BorderColor = BORDER_COLOR;
  58.                 this.DrawFill = true;
  59.                 this.NumberOfColors = 1;
  60.                 this.GradientStyle = GradientStyles.Solid;
  61.                 this.BackColor = BG_COLOR;
  62.                 this.StretchHorizontally = true;
  63.                 this.StretchVertically = true;
  64.             }
  65.             protected override void CreateChildElements()
  66.             {
  67.                 base.CreateChildElements();
  68.                 // arrow
  69.                 this.CreateArrow();
  70.                 // popup
  71.                 this.CreatePopup();
  72.                 this.Children.Add(arrow);
  73.             }
  74.             private void CreatePopup()
  75.             {
  76.                 this.popup = new PopupForm(this);
  77.                 this.popup.PopupClosing += Popup_PopupClosing;
  78.                 this.popup.PopupClosed += Popup_PopupClosed;
  79.             }
  80.             private void Popup_PopupClosing(object sender, RadPopupClosingEventArgs args)
  81.             {
  82.                 // mouse postion in control-bounds prevent
  83.                 if (args.CloseReason == RadPopupCloseReason.Mouse)
  84.                 {
  85.                     var boundsSc = RectangleToScreen(this.Bounds);
  86.                     if (boundsSc.Contains(MousePosition))
  87.                     {
  88.                         args.Cancel = true;
  89.                     }
  90.                 }
  91.             }
  92.             private void Popup_PopupClosed(object sender, RadPopupClosedEventArgs args)
  93.             {
  94.                 if (isInnerCallHide)
  95.                 {
  96.                     return;
  97.                 }
  98.                 this.IsPopupOpen = false;
  99.                 this.SwitchArrowState(false);
  100.             }
  101.             private void CreateArrow()
  102.             {
  103.                 this.arrow = new RadArrowButtonElement()
  104.                 {
  105.                     ClickMode = ClickMode.Press,
  106.                     MinSize = new Size(SystemInformation.VerticalScrollBarWidth,
  107.                                     RadArrowButtonElement.RadArrowButtonDefaultSize.Height),
  108.                     StretchHorizontally = false,
  109.                     StretchVertically = true,
  110.                     Margin = new System.Windows.Forms.Padding(2),
  111.                 };
  112.                 arrow.Fill.NumberOfColors = 1;
  113.                 arrow.Fill.BackColor = ARROW_NORMAL_BG_COLOR;
  114.                 arrow.Border.BoxStyle = BorderBoxStyle.SingleBorder;
  115.                 arrow.Border.GradientStyle = GradientStyles.Solid;
  116.                 arrow.Border.ForeColor = ARROW_BORDER_COLOR;
  117.                 arrow.RadPropertyChanged += Arrow_RadPropertyChanged;
  118.                 arrow.Click += Arrow_Click;
  119.             }
  120.             private void Arrow_Click(object sender, EventArgs e)
  121.             {
  122.                 if (this.IsPopupOpen)
  123.                 {
  124.                     this.IsPopupOpen = false;
  125.                     this.SwitchArrowState(false);
  126.                     this.HidePopup();
  127.                 }
  128.                 else
  129.                 {
  130.                     this.IsPopupOpen = true;
  131.                     this.SwitchArrowState(true);
  132.                     this.ShowPopup();
  133.                 }
  134.             }
  135.             private void HidePopup()
  136.             {
  137.                 this.isInnerCallHide = true;
  138.                 this.popup.Hide();
  139.                 this.isInnerCallHide = false;
  140.             }
  141.             private void ShowPopup()
  142.             {
  143.                 this.popup.Width = this.Bounds.Width;
  144.                 this.popup.Height = 250;
  145.                 this.popup.ShowPopup(this.RectangleToScreen(this.ControlBoundingRectangle));
  146.             }
  147.             private void SwitchArrowState(bool isPressed)
  148.             {
  149.                 this.arrow.Fill.BackColor = isPressed ? ARROW_PRESSED_BG_COLOR :
  150.                     (arrow.IsMouseOver ? ARROW_MOUSE_OVER_BG_COLOR : ARROW_NORMAL_BG_COLOR);
  151.             }
  152.             protected override void OnPropertyChanged(RadPropertyChangedEventArgs e)
  153.             {
  154.                 if (e.Property == ContainsFocusProperty)
  155.                 {
  156.                     var isFocus = (bool)e.NewValue;
  157.                     this.BorderWidth = isFocus ? FOCUS_BORDER_WIDTH : NORMAL_BORDER_WIDTH;
  158.                 }
  159.                 base.OnPropertyChanged(e);
  160.             }
  161.             protected override SizeF ArrangeOverride(SizeF finalSize)
  162.             {
  163.                 base.ArrangeOverride(finalSize);
  164.                 // arrow on right side
  165.                 float width = this.arrow.DesiredSize.Width;
  166.                 float x = this.RightToLeft ? 0f : (finalSize.Width - width);
  167.                 RectangleF finalRect = new RectangleF(x, 0f, width, finalSize.Height);
  168.                 this.arrow.Arrange(finalRect);
  169.                 return finalSize;
  170.             }
  171.             private void Arrow_RadPropertyChanged(object sender, RadPropertyChangedEventArgs e)
  172.             {
  173.                 if (e.Property == RadArrowButtonElement.IsMouseOverProperty)
  174.                 {
  175.                     if (this.IsPopupOpen)
  176.                     {
  177.                         return;
  178.                     }
  179.                     var arrow = sender as RadArrowButtonElement;
  180.                     var isMouseOver = (bool)e.NewValue;
  181.                     arrow.Fill.BackColor = isMouseOver ? ARROW_MOUSE_OVER_BG_COLOR : ARROW_NORMAL_BG_COLOR;
  182.                 }
  183.             }
  184.         }
  185.         public class PopupForm : RadSizablePopupControl
  186.         {
  187.             private HostTreeView tv;
  188.             public PopupForm(RadItem owner)
  189.                 : base(owner)
  190.             {
  191.                 this.SizingMode = SizingMode.UpDownAndRightBottom;
  192.                 this.VerticalAlignmentCorrectionMode = AlignmentCorrectionMode.SnapToOuterEdges;
  193.             }
  194.             public RadTreeView TreeView
  195.             {
  196.                 get
  197.                 {
  198.                     return this.tv.TreeView;
  199.                 }
  200.             }
  201.             protected override void CreateChildItems(RadElement parent)
  202.             {
  203.                 base.CreateChildItems(parent);
  204.                 this.tv = new HostTreeView();
  205.                 this.tv.TreeView.Focusable = false;
  206.                 this.tv.TreeView.CheckBoxes = true;
  207.                 this.SizingGripDockLayout.Children.Add(tv);
  208.             }
  209.             public override bool OnMouseWheel(Control target, int delta)
  210.             {
  211.                 if (delta < 0)
  212.                 {
  213.                     this.tv.TreeView.VScrollBar.PerformSmallIncrement(1);
  214.                 }
  215.                 else
  216.                 {
  217.                     this.tv.TreeView.VScrollBar.PerformSmallDecrement(1);
  218.                 }
  219.                 return true;
  220.             }
  221.         }
  222.         public class HostTreeView : Telerik.WinControls.RadHostItem
  223.         {
  224.             public HostTreeView()
  225.                 : base(new RadTreeView())
  226.             {
  227.             }
  228.             public RadTreeView TreeView
  229.             {
  230.                 get
  231.                 {
  232.                     return this.HostedControl as RadTreeView;
  233.                 }
  234.             }
  235.         }
  236.     }
复制代码
  最后说明一点吧,这次封装对于我自己来说还有一个不满意的地方,那就是选择一些项目以后,界面上不显示已经选择的项,希望有人能够完善一下,给出改造后的代码。

来源:https://www.cnblogs.com/zhenzaizai/p/18120526
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具