Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

File Path Picker Specific Directory

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

    File Path Picker Specific Directory

    Is there a good way to specify a specific drawing tool directory to select the template XML?


    Code:
    [Display(Name = "Template Name", Order = 1, GroupName = "Options")]
    [PropertyEditor("NinjaTrader.Gui.Tools.FilePathPicker", Filter = "Xml Files (*.xml)|*.xml")]
    public string TemplateName
    { get; set; }​

    #2
    Hello ZeroKuhl,

    You can use the InitialDirectory to specify a starting directory, unfortunately variables cannot be used in this case so you would have to hard code the documents path rather than getting the location from the platforms globals.

    Code:
    [Display(Name = "Template Name", Order = 1, GroupName = "Options")]
    [PropertyEditor("NinjaTrader.Gui.Tools.FilePathPicker", Filter = "Xml Files (*.xml)|*.xml", InitialDirectory = @"C:\Users\username\Documents\NinjaTrader 8\templates\DrawingTool")]
    public string TemplateName
    { get; set; }​​

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello ZeroKuhl,

      You can use the InitialDirectory to specify a starting directory, unfortunately variables cannot be used in this case so you would have to hard code the documents path rather than getting the location from the platforms globals.

      Code:
      [Display(Name = "Template Name", Order = 1, GroupName = "Options")]
      [PropertyEditor("NinjaTrader.Gui.Tools.FilePathPicker", Filter = "Xml Files (*.xml)|*.xml", InitialDirectory = @"C:\Users\username\Documents\NinjaTrader 8\templates\DrawingTool")]
      public string TemplateName
      { get; set; }​​


      It is terrible that we can't define paths with variables inside of the NT directory. Is this on the road map to change in the future?

      Comment


        #4
        Hello ZeroKuhl,

        In this case this is more of a C# limitation with how attributes work. Attributes that decorate public properties in C# cannot use any kind of dynamic variable that is generated in runtime, it needs to be a constant. While you can define a specific path using the InitialDirectory that path has to be hard coded to be able to compile. The only way around that would be to create a custom file picker of your own where its property editor uses logic to find the users documents directory

        The following would be how you would approach that, you would have to create a new .cs file in the addons folder to define the custom property editor. This also requires adding a reference to the WpfPropertyGrid dll file in the program files NinjaTrader 8 folder.

        Code:
        requires System.Windows.Controls.WpfPropertyGrid.dll
        
        using System.Windows.Controls.WpfPropertyGrid;
        using System.Windows.Markup;
        
        namespace NinjaTrader.NinjaScript.AddOns
        {
        public class CustomFilePicker : PropertyEditor
        {
        public CustomFilePicker()
        {
        InlineTemplate = CreateTemplate();
        }
        
        DataTemplate CreateTemplate()
        {
        const string xamlTemplate =
        
        @"
        <DataTemplate>
        <Grid>
        <Grid.ColumnDefinitions>
        <ColumnDefinition Width=""30""/>
        <ColumnDefinition Width=""*""/>
        </Grid.ColumnDefinitions>
        <Button Grid.Column=""0"" Content=""..."" Padding=""0"" Margin=""0""
        HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch""
        HorizontalContentAlignment=""Center""
        MinWidth=""30""
        Width=""30""
        MaxWidth=""30""
        Command =""pg:PropertyEditorCommands.ShowDialogEditor""
        CommandParameter=""{Binding}"" />
        <TextBox Grid.Column=""1""
        Text=""{Binding StringValue}""
        ToolTip=""{Binding Value}""/>
        </Grid>
        </DataTemplate>
        ";
        
        ParserContext context = new ParserContext();
        context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
        context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
        context.XmlnsDictionary.Add("pg", "http://schemas.denisvuyka.wordpress.com/wpfpropertygrid");
        DataTemplate template = (DataTemplate)XamlReader.Parse(xamlTemplate, context);
        return template;
        }
        
        public override void ClearValue(PropertyItemValue propertyValue, IInputElement commandSource)
        {
        if (propertyValue == null || propertyValue.IsReadOnly)
        {
        return;
        }
        
        propertyValue.StringValue = string.Empty;
        }
        
        public override void ShowDialog(PropertyItemValue propertyValue, IInputElement commandSource)
        {
        PropertyGrid propGrid = commandSource as PropertyGrid;
        string lastPath = propertyValue.StringValue;
        
        if (propGrid == null) return;
        
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.Filter = "All files (*.*)|*.*";
        openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolde r.MyDocuments);
        openFileDialog.ShowDialog();
        
        propertyValue.StringValue = openFileDialog.FileName != String.Empty ? openFileDialog.FileName : lastPath; // change this string and compile, the ui does not see this change
        
        propGrid.DoReload();
        
        propGrid.RaiseEvent(new PropertyValueChangedEventArgs(PropertyGrid.Propert yValueChangedEvent, propertyValue.ParentProperty, ""));
        }
        }
        }​
        and then use it in your script:

        Code:
        [NinjaScriptProperty]
        [PropertyEditor("NinjaTrader.NinjaScript.AddOns.CustomFilePicker")]
        [Display(Name="Image1 File Path", Description="File Path for Image1", Order=15, GroupName="5. Images")]
        public string Image1Path
        { get; set; }

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        558 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        324 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        545 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        547 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X