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.



    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.



      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 cre8able, Today, 03:20 PM
        0 responses
        2 views
        0 likes
        Last Post cre8able  
        Started by Fran888, 02-16-2024, 10:48 AM
        3 responses
        45 views
        0 likes
        Last Post Sam2515
        by Sam2515
         
        Started by martin70, 03-24-2023, 04:58 AM
        15 responses
        114 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by The_Sec, Today, 02:29 PM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by jeronymite, 04-12-2024, 04:26 PM
        2 responses
        31 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Working...
        X