using cAlgo.API;
using System.Collections.Generic;
namespace cAlgo
{
// このサンプルインジケーターは、Chart Customコントロールを使用して独自のコントロールを作成する方法を示しています
// 複数の組み込みコントロールを組み合わせることで、学習目的のためにコンボボックスコントロールを完全に機能するコンボボックスとして作成しています
[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("アイテム1");
comboBox.AddItem("アイテム2");
comboBox.AddItem("アイテム3");
comboBox.AddItem("アイテム4");
comboBox.AddItem("アイテム5");
Chart.AddControl(comboBox);
}
public override void Calculate(int 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);
}
}
}