Skip to content

CustomControl

Summary

Represents the custom control to create reusable controls with customized look and behavior.

Signature

1
public abstract class CustomControl : ControlBase

Namespace

cAlgo.API

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
 using cAlgo.API;
 using System.Collections.Generic;
 namespace cAlgo
 {
     // This sample indicator shows how to use Chart Custom controls to create your own controls
     // by combining multiple built-in controls, the combobox control is not a fully functioning combobox, its just for learning purpose
     [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class CustomControlSample : Indicator
     {
         protected override void Initialize()
         {
             var comboBox = new ComboBox
             {
                 HorizontalAlignment = HorizontalAlignment.Center,
                 VerticalAlignment = VerticalAlignment.Center
             };
             comboBox.AddItem("Item 1");
             comboBox.AddItem("Item 2");
             comboBox.AddItem("Item 3");
             comboBox.AddItem("Item 4");
             comboBox.AddItem("Item 5");
             Chart.AddControl(comboBox);
         }
         public override void Calculate(int index)
         {
             // Calculate value at specified index
             // Result[index] = ...
         }
     }
     public class ComboBox : CustomControl
     {
         private TextBox _textBox;
         private Button _button;
         private Grid _itemsGrid;
         private StackPanel _panel;
         private readonly List<object> _items = new List<object>();
         private bool _isExpanded;
         public ComboBox()
         {
             _textBox = new TextBox
             {
                 Width = 100,
                 IsReadOnly = true,
                 IsReadOnlyCaretVisible = false
             };
             _button = new Button
             {
                 Text = "▼"
             };
             _button.Click += Button_Click;
             var stackPanel = new StackPanel
             {
                 Orientation = Orientation.Horizontal
             };
             stackPanel.AddChild(_textBox);
             stackPanel.AddChild(_button);
             _panel = new StackPanel
             {
                 Orientation = Orientation.Vertical
             };
             _panel.AddChild(stackPanel);
             AddChild(_panel);
         }
         public void AddItem(object item)
         {
             _items.Add(item);
         }
         public bool RemoveItem(object item)
         {
             return _items.Remove(item);
         }
         private void Button_Click(ButtonClickEventArgs obj)
         {
             if (_itemsGrid != null)
                 _panel.RemoveChild(_itemsGrid);
             if (_isExpanded)
             {
                 _isExpanded = false;
                 return;
             }
             _isExpanded = true;
             _itemsGrid = new Grid(_items.Count, 1);
             for (int i = 0; i < _items.Count; i++)
             {
                 var item = _items[i];
                 _itemsGrid.AddChild(new TextBlock
                 {
                     Text = item.ToString()
                 }, i, 0);
             }
             _panel.AddChild(_itemsGrid);
         }
     }
 }

Last update: July 1, 2022

Comments