SampleWPFModifications

Adding custom WPF Controls to Chart Trader and Modifying existing Chart Trader buttons
 avatar
TrendIsYourFriend
csharp
4 years ago
4.2 kB
40
No Index
protected void CreateWPFControls()
{
	// the main chart window
	chartWindow			= System.Windows.Window.GetWindow(ChartControl.Parent) as Chart;
	// if not added to a chart, do nothing
	if (chartWindow == null)
		return;
	// this is the grid in the chart window
	chartGrid			= chartWindow.MainTabControl.Parent as Grid;
	// this is the entire chart trader area grid
	chartTraderGrid		= (chartWindow.FindFirst("ChartWindowChartTraderControl") as ChartTrader).Content as Grid;

	#region Use case #5: Custom chart trader buttons wpf objects
	// This adds two grid spaces for buttons to the chart trader area. One is below the pnl box (and above the instrument selector, one is below the bid and ask (at the bottom). Rows can be added to either the upper or lower grid to add more buttons in.

	// this grid contains the existing chart trader buttons
	chartTraderGrid.ShowGridLines = true;
	chartTraderButtonsGrid	= chartTraderGrid.Children[0] as Grid;
	//			chartTraderButtonsGrid.ShowGridLines = true;

	// this grid is a grid i'm adding to a new row (at the bottom) in the grid that contains bid and ask prices and order controls (chartTraderButtonsGrid)
	upperButtonsGrid = new Grid();
	Grid.SetColumnSpan(upperButtonsGrid, 3);

	upperButtonsGrid.ColumnDefinitions.Add(new ColumnDefinition());
	upperButtonsGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength((double)Application.Current.FindResource("MarginBase")) }); // separator column
	upperButtonsGrid.ColumnDefinitions.Add(new ColumnDefinition());

	// this grid is to organize stuff below
	lowerButtonsGrid = new Grid();
	Grid.SetColumnSpan(lowerButtonsGrid, 4);

	lowerButtonsGrid.ColumnDefinitions.Add(new ColumnDefinition());
	lowerButtonsGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength((double)Application.Current.FindResource("MarginBase")) });
	lowerButtonsGrid.ColumnDefinitions.Add(new ColumnDefinition());

	// these rows will be added later, but we can create them now so they only get created once
	customCtaddedRow1	= new RowDefinition() { Height = new GridLength(31) };
	customCtaddedRow2	= new RowDefinition() { Height = new GridLength(40) };

	// this style (provided by NinjaTrader_MichaelM) gives the correct default minwidth (and colors) to make buttons appear like chart trader buttons

	// all of the buttons are basically the same so to save lines of code I decided to use a loop over an array
	chartTraderCustomButtonsArray = new Button[4];

	for (int i = 0; i < 4; ++i)
	{
		chartTraderCustomButtonsArray[i]	= new Button()
		{
			Content			= string.Format("Button {0}", i + 1),
			Height			= 30,
			Margin			= new Thickness(0,0,0,0),
			Padding			= new Thickness(0,0,0,0),
			Style			= chartTraderButtonStyle
		};

		// change colors of the buttons if you'd like. i'm going to change the first and fourth.
		if (i % 3 != 0)
		{
			chartTraderCustomButtonsArray[i].Background		= Brushes.Gray;
			chartTraderCustomButtonsArray[i].BorderBrush	= Brushes.DimGray;
		}
	}

	chartTraderCustomButtonsArray[0].Click += ChartTraderButtonMenu_Click;
	chartTraderCustomButtonsArray[1].Click += ChartTraderButtonMenu_Click;
	chartTraderCustomButtonsArray[2].Click += ChartTraderButtonMenu_Click;
	chartTraderCustomButtonsArray[3].Click += ChartTraderButtonMenu_Click;

	Grid.SetColumn(chartTraderCustomButtonsArray[1], 2);
	// add button3 to the lower grid
	Grid.SetColumn(chartTraderCustomButtonsArray[2], 0);
	// add button4 to the lower grid
	Grid.SetColumn(chartTraderCustomButtonsArray[3], 2);
	for (int i = 0; i < 2; ++i)
		upperButtonsGrid.Children.Add(chartTraderCustomButtonsArray[i]);
	for (int i = 2; i < 4; ++i)
		lowerButtonsGrid.Children.Add(chartTraderCustomButtonsArray[i]);
	#endregion

	#region Use case #6: Modify existing chart trader buttons wpf objects

	modifyCtBuyMarketButton			= chartTraderGrid.FindFirst("ChartTraderControlQuickBuyMarketButton") as Button;
	modifyCtSellMarketButton		= chartTraderGrid.FindFirst("ChartTraderControlQuickSellMarketButton") as Button;
	#endregion

	if (TabSelected())
		ShowWPFControls();

	chartWindow.MainTabControl.SelectionChanged += TabChangedHandler;
}
Editor is loading...