public override void OnRestoreValues() { bool restored = false; if (NotesSerializable != null) { foreach (string note in NotesSerializable) { string[] noteVal = note.Split(';'); double price; if (double.TryParse(noteVal[0], NumberStyles.Any, CultureInfo.InvariantCulture, out price)) { PriceStringValues.AddOrUpdate(price, noteVal[1], (key, old) => noteVal[1]); restored = true; } } } if (restored) { OnPropertyChanged(); } }
protected override void OnStateChange() { if (State == State.SetDefaults) { Name = NinjaTrader.Custom.Resource.NinjaScriptSuperDomColumnNotes; Description = NinjaTrader.Custom.Resource.NinjaScriptSuperDomColumnDescriptionNotes; DefaultWidth = 160; PreviousWidth = -1; IsDataSeriesRequired = false; BackColor = Application.Current.TryFindResource("brushPriceColumnBackground") as Brush; ForeColor = Application.Current.TryFindResource("FontControlBrush") as Brush; NotesSerializable = new List <string>(); PriceStringValues = new ConcurrentDictionary <double, string>(); } else if (State == State.Configure) { if (UiWrapper != null && PresentationSource.FromVisual(UiWrapper) != null) { Matrix m = PresentationSource.FromVisual(UiWrapper).CompositionTarget.TransformToDevice; double dpiFactor = 1 / m.M11; gridPen = new Pen(Application.Current.TryFindResource("BorderThinBrush") as Brush, 1 * dpiFactor); halfPenWidth = gridPen.Thickness * 0.5; } tbNotes = new TextBox { Margin = new Thickness(0), VerticalAlignment = VerticalAlignment.Top, Visibility = Visibility.Hidden }; SetBindings(); tbNotes.LostKeyboardFocus += (o, args) => { if (currentEditingPrice != -1.0 && tbNotes.Visibility == Visibility.Visible) { SetAndSaveNote(); tbNotes.Text = string.Empty; currentEditingPrice = -1.0; tbNotes.Visibility = Visibility.Hidden; OnPropertyChanged(); } }; tbNotes.KeyDown += (o, args) => { if (args.Key == Key.Enter || args.Key == Key.Tab) { SetAndSaveNote(); tbNotes.Text = string.Empty; currentEditingPrice = -1.0; tbNotes.Visibility = Visibility.Hidden; OnPropertyChanged(); } else if (args.Key == Key.Escape) { currentEditingPrice = -1.0; tbNotes.Visibility = Visibility.Hidden; OnPropertyChanged(); } }; } else if (State == State.Active) { foreach (PriceRow row in SuperDom.Rows) { PriceStringValues.AddOrUpdate(row.Price, string.Empty, (key, oldValue) => oldValue); } } else if (State == State.Terminated) { if (UiWrapper != null) { UiWrapper.Children.Remove(tbNotes); UiWrapper.InputBindings.Remove(doubleClickMouseBinding); UiWrapper.CommandBindings.Remove(displayTextBoxCommandBinding); } } }
protected override void OnRender(DrawingContext dc, double renderWidth) { // This may be true if the UI for a column hasn't been loaded yet (e.g., restoring multiple tabs from workspace won't load each tab until it's clicked by the user) if (gridPen == null) { if (UiWrapper != null && PresentationSource.FromVisual(UiWrapper) != null) { Matrix m = PresentationSource.FromVisual(UiWrapper).CompositionTarget.TransformToDevice; double dpiFactor = 1 / m.M11; gridPen = new Pen(Application.Current.TryFindResource("BorderThinBrush") as Brush, 1 * dpiFactor); halfPenWidth = gridPen.Thickness * 0.5; } } columnWidth = renderWidth; gridHeight = -gridPen.Thickness; double verticalOffset = -gridPen.Thickness; // If SuperDom scrolls so that editing price goes off the grid, hide the textbox until the editing price is visible again if (SuperDom.IsConnected) { if (tbNotes.Visibility == Visibility.Visible && SuperDom.Rows.All(r => r.Price != currentEditingPrice)) { tbNotes.Visibility = Visibility.Hidden; } if (tbNotes.Visibility == Visibility.Hidden && SuperDom.Rows.Any(r => r.Price == currentEditingPrice)) { tbNotes.Visibility = Visibility.Visible; } } lock (SuperDom.Rows) foreach (PriceRow row in SuperDom.Rows) { // Add new prices if needed to the dictionary as the user scrolls PriceStringValues.AddOrUpdate(row.Price, string.Empty, (key, oldValue) => oldValue); // If textbox is open, move it when the SuperDom scrolls if (tbNotes.Visibility == Visibility.Visible && row.Price == currentEditingPrice) { if (SuperDom.Rows.IndexOf(row) != gridIndex) { gridIndex = SuperDom.Rows.IndexOf(row); double tbOffset = gridIndex * SuperDom.ActualRowHeight; tbNotes.Margin = new Thickness(0, tbOffset, 0, 0); } } // Draw cell if (renderWidth - halfPenWidth >= 0) { Rect rect = new Rect(-halfPenWidth, verticalOffset, renderWidth - halfPenWidth, SuperDom.ActualRowHeight); // Create a guidelines set GuidelineSet guidelines = new GuidelineSet(); guidelines.GuidelinesX.Add(rect.Left + halfPenWidth); guidelines.GuidelinesX.Add(rect.Right + halfPenWidth); guidelines.GuidelinesY.Add(rect.Top + halfPenWidth); guidelines.GuidelinesY.Add(rect.Bottom + halfPenWidth); dc.PushGuidelineSet(guidelines); dc.DrawRectangle(BackColor, null, rect); dc.DrawLine(gridPen, new Point(-gridPen.Thickness, rect.Bottom), new Point(renderWidth - halfPenWidth, rect.Bottom)); dc.DrawLine(gridPen, new Point(rect.Right, verticalOffset), new Point(rect.Right, rect.Bottom)); // Print note value - remember to set MaxTextWidth so text doesn't spill into another column string note; if (PriceStringValues.TryGetValue(row.Price, out note) && !string.IsNullOrEmpty(PriceStringValues[row.Price])) { fontFamily = SuperDom.Font.Family; typeFace = new Typeface(fontFamily, SuperDom.Font.Italic ? FontStyles.Italic : FontStyles.Normal, SuperDom.Font.Bold ? FontWeights.Bold : FontWeights.Normal, FontStretches.Normal); if (renderWidth - 6 > 0) { FormattedText noteText = new FormattedText(note, Core.Globals.GeneralOptions.CurrentCulture, FlowDirection.LeftToRight, typeFace, SuperDom.Font.Size, ForeColor) { MaxLineCount = 1, MaxTextWidth = renderWidth - 6, Trimming = TextTrimming.CharacterEllipsis }; dc.DrawText(noteText, new Point(0 + 4, verticalOffset + (SuperDom.ActualRowHeight - noteText.Height) / 2)); } } dc.Pop(); verticalOffset += SuperDom.ActualRowHeight; gridHeight += SuperDom.ActualRowHeight; } } }