Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Add a property editor to an NTWindow

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Add a property editor to an NTWindow

    I'm trying to display a property editor for an object that is not an indicator.
    The object may have nested objects and definitely has Brush (color) properties. I'd like the property editor to behave like the property editor for an indicator.
    Is there a way to add a NinjaTrader property editor to an NTWindow to edit my object? Here's a little example code that needs to have the "blanks" filled in.

    public class Settings {
    public int A { get; set; }
    public SubSettings B { get; set; }
    public Brush C { get; set; }
    public Settings() {
    A = 1;
    B = new SubSettings();
    C = new SolidColorBrush(Colors.WhiteSmoke);
    }
    }

    public class SubSettings {
    public int A { get; set; }
    public Brush B { get; set; }
    public SubSettings() {
    A = 10;
    B = new SolidColorBrush(Colors.Red);
    }
    }

    class PropertyEditorWindow : NTWindow {
    Settings _editingObject;
    public PropertyEditorWindow() {
    Caption = "Editor";
    // add some control here to display properties
    }
    public Settings EditingObject {
    get { return _editingObject; }
    set {
    _editingObject = value;
    // do something here to display the properties of this settings object
    }
    }
    }

    static class ExampleUsage {
    public static void DoExample() {
    var settings = new Settings();
    var editor = new PropertyEditorWindow();
    editor.EditingObject = settings;
    editor.Show();
    }
    }
    Many thanks!

    #2
    Thank you for your question bboyle1234.

    Ninja does not have a public documented PropertyEditor screen, and so you will need to create this window in XAML using the guidelines here,


    Once this is set up, one challenge you will run into is serializing and unserializing brushes by name, for example, "ForestGreen" in Brushes.ForestGreen . The attached code will help with this, and should serve as a guide for marshalling (serializing) other objects. These six lines of code from this sample will help you set up a color selector as well, since they loop through all the named colors :

    Code:
    [FONT=Courier New]
                Type colors = typeof(System.Windows.Media.Colors);
                foreach(var prop in colors.GetProperties())
                {
                    if(((System.Windows.Media.Color)prop.GetValue(null, null)) == color)
                        return prop.Name;
                }
    [/FONT]
    We are happy to help with any questions that come up during the process of setting up this screen.
    Attached Files
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      There is one more hint I can provide. You mentioned needing nested menus as well. This publicly available tutorial will demonstrate creating tree views in C# with WPF.

      Jessica P.NinjaTrader Customer Service

      Comment


        #4
        NinjaTrader uses the WPFPropertyGrid by DenisVuyka.
        WPF PropertyGrid Control. Contribute to DenysVuika/WPG development by creating an account on GitHub.


        You assign an object to display in the control through its SelectedObject property.

        Comment


          #5
          Thanks to all who contributed, here is the solution I ended up using:
          Code:
          using NinjaTrader.Gui.Tools;
          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;
          using System.Windows.Media;
          using System.Windows.Controls.WpfPropertyGrid;
          using System.Windows.Controls;
          using System.Windows;
          
          namespace NinjaTrader.Custom.AddOns {
              class PropertyEditorWindow : NTWindow {
          
                  PropertyEditorWindow(string caption, object editingObject) {
                      Caption = caption;
                      Width = 400;
                      Height = 600;
          
                      var grid = new Grid();
                      Content = grid;
                      grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
                      grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
                      grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(40, GridUnitType.Pixel) });
          
                      var scrollViewer = new ScrollViewer();
                      grid.Children.Add(scrollViewer);
                      Grid.SetColumn(scrollViewer, 0);
                      Grid.SetRow(scrollViewer, 0);
                      scrollViewer.HorizontalAlignment = HorizontalAlignment.Stretch;
                      scrollViewer.VerticalAlignment = VerticalAlignment.Stretch;
          
                      var pg = new PropertyGrid();
                      scrollViewer.Content = pg;
                      pg.HorizontalAlignment = HorizontalAlignment.Stretch;
                      pg.VerticalAlignment = VerticalAlignment.Stretch;
                      pg.SelectedObject = editingObject;
          
                      var buttonStackPanel = new StackPanel();
                      grid.Children.Add(buttonStackPanel);
                      Grid.SetColumn(buttonStackPanel, 0);
                      Grid.SetRow(buttonStackPanel, 1);
                      buttonStackPanel.Orientation = Orientation.Horizontal;
                      buttonStackPanel.HorizontalAlignment = HorizontalAlignment.Right;
                      buttonStackPanel.FlowDirection = FlowDirection.RightToLeft;
          
                      var buttonStyle = Application.Current.TryFindResource("SuperDomQuickButton") as Style;
          
                      var bCancel = new Button {
                          Content = "Cancel",
                          Style = buttonStyle,
                          Margin = new Thickness(10, 0, 10, 0),
                          IsCancel = true,
                          Width = 98,
                      };
                      buttonStackPanel.Children.Add(bCancel);
          
                      var bOK = new Button {
                          Content = "OK",
                          Style = buttonStyle,
                          Margin = new Thickness(10, 0, 10, 0),
                          IsDefault = true,
                          Width = 98,
                      };
                      buttonStackPanel.Children.Add(bOK);
                      bOK.Click += (s, e) => {
                          DialogResult = true;
                          Close();
                      };
          
                  }
                  public static bool TryEdit(Window owner, string caption, object selectedObject) {
                      var window = new PropertyEditorWindow(caption, selectedObject);
                      window.Owner = owner;
                      return window.ShowDialog() ?? false;
                  }
              }
          }

          Comment


            #6
            This is excellent. Thanks.

            Do you have an example of usage of this, preferably within the AddOn context, please? Also, I note that one needs to add a Reference to the System.Windows.Controls.WpfPropertyGrid dll in the NinjaTrader 8 Program Files (x86) folder.

            Thanks.
            Multi-Dimensional Managed Trading
            jeronymite
            NinjaTrader Ecosystem Vendor - Mizpah Software

            Comment


              #7
              Here's how I used it years ago, inside an add-on window that displayed news. A menu button in the add-on window launched the property editor.
              Note that the settings object has a good Clone method and an Equals method that determines equality based on all the internal fields being equal.

              Code:
              mnuEdit.Click += (s, e) => {
                  var tempSettings = _settings.Clone();
                  if (PropertyEditorWindow.TryEdit(this, "News window preferences", tempSettings)) {
                      if (!tempSettings.Equals(_settings)) {
                          _settings = tempSettings;
                          BuildView();
                      }
                  }
              };

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Christopher_R, Today, 12:29 AM
              0 responses
              9 views
              0 likes
              Last Post Christopher_R  
              Started by sidlercom80, 10-28-2023, 08:49 AM
              166 responses
              2,235 views
              0 likes
              Last Post sidlercom80  
              Started by thread, Yesterday, 11:58 PM
              0 responses
              3 views
              0 likes
              Last Post thread
              by thread
               
              Started by jclose, Yesterday, 09:37 PM
              0 responses
              8 views
              0 likes
              Last Post jclose
              by jclose
               
              Started by WeyldFalcon, 08-07-2020, 06:13 AM
              10 responses
              1,415 views
              0 likes
              Last Post Traderontheroad  
              Working...
              X