Using BitmapImage objects with Buttons

View as Markdown

Images as Buttons Overview

BitmapImage objects can be used to apply an image as a background to a Button object added to a NinjaTrader window.

The following topic covers methods and properties outside of the NinjaScript libraries. Most of the items covered in the example below belong to .NET’s System.Windows.Media.Imaging and System.Windows.Controls namespaces. More information on these namespaces can be found at the links below:

Using an image as the background for a button can be achieved through a fairly straightforward process using some of the .NET framework’s Controls and Imaging methods

There are a few best practices to keep in mind when working with Buttons:

  • Dispose of any leftover objects in State.Terminated for efficient memory use

  • Use your object’s main Dispatcher when adding or removing Buttons to or from your chart, to ensure that the correct thread is used

  • Be aware of the proper States in which to initialize objects related to the Button (State.Configure), apply the Button (State.Historical), and dispose of unneeded objects (State.Terminated)

Adding a Button to a Chart Toolbar Using an Image as the Background

The example below walks through the process of adding a Button to a chart toolbar specifically, and applying a .jpg image as the Button’s background. This example also displays several best practices when working with Buttons, such as proper object disposal and ensuring that the Button is not populated when the indicator is applied in an inactive chart tab.

1//Add the following Using statements
2using System.Windows.Media.Imaging;
3using System.Windows.Controls;
4
5public class addButton : Indicator
6{
7 // Define a Chart object to refer to the chart on which the indicator resides
8 private Chart chartWindow;
9
10 // Define a Button
11 private System.Windows.Controls.Button myButton = null;
12
13 // Instantiate a BitmapImage to hold an image
14 BitmapImage myBitmapImage = new BitmapImage();
15
16 // Instantiate an ImageBrush to apply to the Button
17 ImageBrush backgroundImage = new ImageBrush();
18
19 private bool IsToolBarButtonAdded;
20
21 protected override void OnStateChange()
22 {
23 if (State == State.Configure)
24 {
25 // Assign an image on the filesystem to the BitmapImage.
26
27 // This example assumes that a jpg image named "ButtonBackground" resides in the install directory
28 myBitmapImage.BeginInit();
29 myBitmapImage.UriSource = new Uri(NinjaTrader.Core.Globals.InstallDir + "ButtonBackground.jpg");
30 myBitmapImage.EndInit();
31
32 // Assign the BitmapImage as the ImageSource of the ImageBrush
33 backgroundImage.ImageSource = myBitmapImage;
34 }
35 else if (State == State.Historical)
36 {
37 //Call the custom addButtonToToolbar method in State.Historical to ensure it is only done when applied to a chart
38
39 // -- not when loaded in the Indicators window
40 if (!IsToolBarButtonAdded) AddButtonToToolbar();
41 }
42 else if (State == State.Terminated)
43 {
44 //Call a custom method to dispose of any leftover objects in State.Terminated
45 DisposeCleanUp();
46 }
47 }
48
49 private void AddButtonToToolbar()
50 {
51 // Use this.Dispatcher to ensure code is executed on the proper thread
52 ChartControl.Dispatcher.InvokeAsync((Action)(() =>
53 {
54
55 //Obtain the Chart on which the indicator is configured
56 chartWindow = Window.GetWindow(this.ChartControl.Parent) as Chart;
57 if (chartWindow == null)
58 {
59 Print("chartWindow == null");
60 return;
61 }
62
63 // Create a style to apply to the button
64 Style s = new Style();
65 s.TargetType = typeof(System.Windows.Controls.Button);
66 s.Setters.Add(new Setter(System.Windows.Controls.Button.FontSizeProperty, 11.0));
67 s.Setters.Add(new Setter(System.Windows.Controls.Button.BackgroundProperty, Brushes.Orange));
68 s.Setters.Add(new Setter(System.Windows.Controls.Button.ForegroundProperty, Brushes.Black));
69 s.Setters.Add(new Setter(System.Windows.Controls.Button.FontFamilyProperty, new FontFamily("Arial")));
70 s.Setters.Add(new Setter(System.Windows.Controls.Button.FontWeightProperty, FontWeights.Bold));
71
72 // Instantiate the Button
73 myButton = new System.Windows.Controls.Button();
74
75 //Set Button Style
76 myButton.Style = s;
77
78 // Set the Imagebrush as the Background for the Button
79 myButton.Background = backgroundImage;
80
81 myButton.Content = "Click Here";
82 myButton.IsEnabled = true;
83 myButton.HorizontalAlignment = HorizontalAlignment.Left;
84
85 // Add the Button to the Chart's Toolbar
86 chartWindow.MainMenu.Add(myButton);
87
88 //Prevent the Button From Displaying when WorkSpace Opens if it is not in an active tab
89 myButton.Visibility = Visibility.Collapsed;
90 foreach (TabItem tab in this.chartWindow.MainTabControl.Items)
91 {
92 if ((tab.Content as ChartTab).ChartControl == this.ChartControl
93
94 && tab == this.chartWindow.MainTabControl.SelectedItem)
95 {
96 myButton.Visibility = Visibility.Visible;
97 }
98 }
99 IsToolBarButtonAdded = true;
100 }));
101 }
102
103 private void DisposeCleanUp()
104 {
105 //ChartWindow Null Check
106 if (chartWindow != null)
107 {
108 //Dispatcher used to Assure Executed on UI Thread
109 ChartControl.Dispatcher.InvokeAsync((Action)(() =>
110 {
111 //Button Null Check
112 if (myButton != null)
113 {
114 //Remove Button from Indicator's Chart ToolBar
115 chartWindow.MainMenu.Remove(myButton);
116 }
117 }));
118 }
119 }
120}