I have designed a toolbar manager that can hide or show each of the buttons in the main menu toolbar. All works fine except for one bug that i ( and Claude) can’t seem to solve. The ChartTrader and Crosshair buttons often go greyed out. Anyone have any ideas about this - current thinking is that there is a method called canExecute which is overriding the toolbar bool i have set.
Its annoying because it works most of the time and i can’t analyse what turns it on or off ![]()
Here is the core of the indicator which i will post in the ecosystem if i can solve this issue.
#region OnRender for use in Indicator
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
{
if (toolbarModified) return;
ChartControl.Dispatcher.InvokeAsync(() =>
{
var chartWindow = Window.GetWindow(ChartControl.Parent) as Chart;
if (chartWindow == null) return;
bool all = true;
var toHide = new HashSet<string>();
if (all ? !RemoveInstrumentSelector : RemoveInstrumentSelector) toHide.Add("instrumentSelector");
if (all ? !RemoveIntervalSelector : RemoveIntervalSelector) toHide.Add("intervalSelector");
if (all ? !RemoveStrategies : RemoveStrategies) toHide.Add("miStrategies");
if (all ? !RemoveIndicators : RemoveIndicators) toHide.Add("miIndicators");
if (all ? !RemoveDataBox : RemoveDataBox) toHide.Add("miDataBox");
if (all ? !RemoveProperties : RemoveProperties) toHide.Add("miProperties");
if (all ? !RemoveZoomOut : RemoveZoomOut) toHide.Add("miZoomOut");
if (all ? !RemoveZoomIn : RemoveZoomIn) toHide.Add("miZoomIn");
if (all ? !RemoveDataSeries : RemoveDataSeries) toHide.Add("miDataSeries");
if (all ? !RemoveDrawingTool : RemoveDrawingTool) toHide.Add("miDrawingTool");
if (all ? !RemoveChartStyle : RemoveChartStyle) toHide.Add("miChartStyle");
if (all ? !RemoveCrosshair : RemoveCrosshair) toHide.Add("miCrosshair");
if (all ? !RemoveChartTrader : RemoveChartTrader) toHide.Add("miChartTrader");
foreach (var item in chartWindow.MainMenu)
ApplyVisibility(item, toHide);
toolbarModified = true;
});
}
private void ApplyVisibility(object item, HashSet<string> toHide)
{
if (!(item is System.Windows.UIElement el)) return;
var itemName = item.GetType().GetProperty("Name")?.GetValue(item)?.ToString();
if (!string.IsNullOrEmpty(itemName) && toHide.Contains(itemName))
{
el.Visibility = System.Windows.Visibility.Collapsed;
return;
}
el.ClearValue(System.Windows.UIElement.VisibilityProperty);
el.ClearValue(System.Windows.UIElement.IsEnabledProperty);
var children = item.GetType().GetProperty("Items")?.GetValue(item) as System.Collections.IEnumerable;
if (children == null) return;
foreach (var child in children)
ApplyVisibility(child, toHide);
}
private void RestoreToolbar()
{
var cc = cachedChartControl;
if (cc == null) return;
cc.Dispatcher.InvokeAsync(() =>
{
var chartWindow = Window.GetWindow(cc.Parent) as Chart;
if (chartWindow == null) return;
foreach (var item in chartWindow.MainMenu)
ResetElement(item);
System.Windows.Input.CommandManager.InvalidateRequerySuggested();
});
}
private void ResetElement(object item)
{
if (!(item is System.Windows.UIElement el)) return;
el.ClearValue(System.Windows.UIElement.VisibilityProperty);
el.ClearValue(System.Windows.UIElement.IsEnabledProperty);
var children = item.GetType().GetProperty("Items")?.GetValue(item) as System.Collections.IEnumerable;
if (children == null) return;
foreach (var child in children)
ResetElement(child);
}
#endregion