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

Implementing INotifyPropertyChanged for WPF in Ninjascript

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

    Implementing INotifyPropertyChanged for WPF in Ninjascript

    I have a strategy that adds comboxes to the ChartWindow MainMenu. Changing the selections in the comboxes changes the indicator's behavior. This works. However, I want to be able to programatically change the SelectedItem of the combobox. That is if I change the value of the underlying property the control is bound to, the combobox will update to refelct the change. To do this I must implement the INotifyPropertyChanged interface. When I implement this the PropertyChanged handler is always null because the DataContext is not set. How do I properly set the datacontext so that I can properly implement traditional 2 way wpf bindings?

    #2
    Hello TheWhiteDragon,

    Thanks for your post.

    WPF modifications are not specifically NinjaScript, so external resources on WPF could be useful for further direction.

    I have included a link to one such publicly available resource that can demonstrate binding a ComboBox with INotifyPropertyChanged.

    https://blogs.msdn.microsoft.com/dev...opertychanged/

    Another approach could be to add event handlers to each ComboBox's SelectionChanged event. I have attached an example that demonstrates adding event handlers to detect changes to a ComboBox and to modify another Combo Box.

    Please let us know if we can be of further assistance.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jim View Post
      Hello TheWhiteDragon,

      Thanks for your post.

      WPF modifications are not specifically NinjaScript, so external resources on WPF could be useful for further direction.

      I have included a link to one such publicly available resource that can demonstrate binding a ComboBox with INotifyPropertyChanged.

      https://blogs.msdn.microsoft.com/dev...opertychanged/

      Another approach could be to add event handlers to each ComboBox's SelectionChanged event. I have attached an example that demonstrates adding event handlers to detect changes to a ComboBox and to modify another Combo Box.

      Please let us know if we can be of further assistance.
      So to use the link that you posted as an example the part that I'm having trouble with is this:

      Code:
      public ComboBox() 
              { 
                  InitializeComponent(); 
                  DataContext = this;
      So you can't set the DataContext to this because indicator is not a type with a DataContext.

      Comment


        #4
        Hello TheWhiteDragon,

        Correct, DataContext would not be available directly from an indicator/strategy as they are not inherited with that object. DataContext would be available in the Control itself and you can create your own view model to bind with. This is similar to the guide linked previously, but the guide below would be more accurate as it does not involve InitializeComponent() or DataContext directly.

        How to: Create a Binding in Code: - https://docs.microsoft.com/en-us/dot...inding-in-code

        I have also attached a sample that can demonstrate usage in a NinjaScript strategy. This should work in the context of an indicator as well.

        Please note that the code used in this example is not documented/supported NinjaScript.

        I look forward to being of further assistance.
        Attached Files
        Last edited by NinjaTrader_Jim; 04-05-2021, 08:50 AM.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by dwsaqs, Today, 04:40 AM
        0 responses
        1 view
        0 likes
        Last Post dwsaqs
        by dwsaqs
         
        Started by pjsmith, Today, 04:30 AM
        0 responses
        2 views
        0 likes
        Last Post pjsmith
        by pjsmith
         
        Started by Tresafi, Today, 02:20 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Jason  
        Started by Giorgetto, Today, 03:45 AM
        0 responses
        4 views
        0 likes
        Last Post Giorgetto  
        Started by Cardn, Today, 03:32 AM
        0 responses
        8 views
        0 likes
        Last Post Cardn
        by Cardn
         
        Working...
        X