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

Dynamic Property Update in AddOn

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

    Dynamic Property Update in AddOn

    I want to add a clock to my AddOn as a simple TextBlock. It will simply update from a string source (clock) in a completely separate namespace (Timespace).

    I have added the following NTTabPage:
    Code:
        public class AddOnTabClock : NTTabPage, NinjaTrader.Gui.Tools.IInstrumentProvider, NinjaTrader.Gui.Tools.IIntervalProvider, INotifyPropertyChanged
        {
            private TextBlock MyClock = new TextBlock();
            private Cbi.Instrument         instrument;
    
            public AddOnTabClock()
            {
                Content = LoadXAML();
                TabName = "CLOCK";
            }
    
            private DependencyObject LoadXAML()
            {
                try
                {
                    using (System.IO.Stream assemblyResourceStream = GetManifestResourceStream("AddOns.TestClockPanel.xaml"))
                    {
                        if (assemblyResourceStream == null)
                            return null;
    
                        System.IO.StreamReader streamReader = new System.IO.StreamReader(assemblyResourceStream);
    
                        Page page = System.Windows.Markup.XamlReader.Load(streamReader.BaseStream) as Page;
    
                        if (page == null)
                            return  null;
    
                        DependencyObject pageContent = page.Content as DependencyObject;
    
                        MyClock = LogicalTreeHelper.FindLogicalNode(pageContent, "MyClock")        as TextBlock;
    
                        MyClock.SetBinding(TextBlock.TextProperty, new Binding { Source = Timespace.clock, Path = new PropertyPath("Text")});
    
                        return pageContent;
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void OnPropertyChanged(string propName)
            {
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
    
            public Cbi.Instrument Instrument
            {
                get { return instrument; }
                set { PropagateInstrumentChange(value); }
            }
    
            public NinjaTrader.Data.BarsPeriod BarsPeriod
            { get; set; }
    
            protected override string GetHeaderPart(string variable)
            {
                return variable ;
            }
    
            public override void Cleanup()
            {
                base.Cleanup();
            }
    
            protected override void Restore(XElement element)
            {
                if (element == null)
                    return;
            }
    
            protected override void Save(XElement element)
            {
                if (element == null)
                    return;
            }
        }
    The XAML is:
    Code:
    <Page    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:t="clr-namespace:NinjaTrader.Gui.Tools;assembly=NinjaTrader.Gui"
            xmlns:system="clr-namespace:System;assembly=mscorlib"
            >
        <Grid>
            <TextBlock x:Name="MyClock" Text="REAL Time here!"/>
        </Grid>
    </Page>
    Initially, I just want to get the binding to work so I can display a fixed time that is set into the variable in the separate namespace, to show that the binding is working. I do the above which should result in the Timespace.clock string being displayed, but it isn't happening. I print the result of the LogicalTreeHelper (i.e. MyClock.Text, and it is, as expected, "REAL Time here!"), and I print the Timespace.clock string (it is, as set, "Today is Thursday"). But when I Print MyClock.Text after the Binding, it is blank, and so is the tab.

    What am I missing?

    Thanks!
    Last edited by jeronymite; 04-01-2020, 11:23 PM. Reason: Added tags
    Multi-Dimensional Managed Trading
    jeronymite
    NinjaTrader Ecosystem Vendor - Mizpah Software

    #2
    Hello jeronymite,

    Generally if the tab is blank that means the content failed to load, you might be hitting an error in the code you added.

    As for the binding, the way the addon loads the xaml is not a good situation to use bindings. Because it parses the text of the xaml and builds it into an object it is not associated with a view model to have any kind of context. Bindings would need to be created manually and would end up being more complex code than just using the object you already have. I would not suggest trying to bind data in NinjaScript. You can instead use a Timer for this or a Method which just directly sets the text:

    Code:
    [B]MyClock[/B].Text = "time";
    If you want to use bindings I would suggest to first explore that in an external WPF test application so that you have a full understanding of what all is required for using bindings in a normal WPF environment. You can then explore building bindings manually in that environment to fully understand that. Once you have those type of understandings/tests you could try to bring it into NinjaScript however you will end up using more code/effort by doing that in this use case. NinjaTrader does not compile the xaml so it is not linked in any way with your code, you instead parse the xaml and pull elements from it that are required.

    If you need to use all of the normal WPF/xaml features you can also use the addon visual studio project as that would allow you to create normal xaml pages or bindings. This is outside of what we can assist with and relies on your WPF/xaml knowledge to do that, this also is a slow development process because you have to restart the platform for each build.



    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks, Jesse, as always.

      The tab is blank only because what I display in it has been blanked in the binding. If I simply omit the binding, the text displays as expected; so it is the binding process that is causing the issue.

      Also, what I am trying to do is merely a proof-of-concept example of what I need to be able to do with binding. I have some dozens of data that have corresponding display items in the AddOn windows/tabs, so having dynamic property updating via binding is essential. I am migrating some 80,000 lines of Winforms code from NT7, so using binding for dynamic updating is crucial.

      Grateful for your kind assistance on achieving this reliably in NinjaScript.

      Thanks!
      Last edited by jeronymite; 04-02-2020, 02:58 PM.
      Multi-Dimensional Managed Trading
      jeronymite
      NinjaTrader Ecosystem Vendor - Mizpah Software

      Comment


        #4
        Hello jeronymite,

        If that is crucial I would likely suggest to just make a WPF dll in visual studio outside of NinjaTrader. That would allow you to use the normal xaml/wpf mechanics like you are trying to do. A Page being loaded and parsed is really not intended to be bound or have bindings in its xaml. You would have to build all of that part of the bindings yourself in C# code and then apply it to the various controls in the page, very similar to what is already being done but it would be more involved. This is not something that our support could help with or provide guidance to make it reliable. The addon sample in the help guide demonstrates the reliable way to create a xaml Page and use its controls.




        I look forward to being of further assistance.



        JesseNinjaTrader Customer Service

        Comment


          #5
          Thanks, Jesse. Understood.
          Multi-Dimensional Managed Trading
          jeronymite
          NinjaTrader Ecosystem Vendor - Mizpah Software

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Waxavi, Today, 02:10 AM
          0 responses
          6 views
          0 likes
          Last Post Waxavi
          by Waxavi
           
          Started by TradeForge, Today, 02:09 AM
          0 responses
          11 views
          0 likes
          Last Post TradeForge  
          Started by Waxavi, Today, 02:00 AM
          0 responses
          2 views
          0 likes
          Last Post Waxavi
          by Waxavi
           
          Started by elirion, Today, 01:36 AM
          0 responses
          4 views
          0 likes
          Last Post elirion
          by elirion
           
          Started by gentlebenthebear, Today, 01:30 AM
          0 responses
          5 views
          0 likes
          Last Post gentlebenthebear  
          Working...
          X