Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Brush property - Error on getting value for property.

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

    Brush property - Error on getting value for property.

    I have an indicator with a Brush property that is used to draw lines on the indicator panel with the specified color (brush).

    In the SetDefaults section of OnStateChange, if I set LineColor = Brushes.Red, indicator works fine.

    However, if I set using the FromRgb method,

    Code:
    LineColor = new SolidColorBrush(Color.FromRgb(100, 125, 150));
    the LineColor property shows the specified color in the settings tab. But when I click Apply / OK, I see the following error -

    Error on getting value for property 'LineColor'. The calling thread cannot access this object because a different thread owns it.


    I think the issue is with assigning SolidColorBrush to the property during initialization.

    How do I fix it ?


    Code:
            [NinjaScriptProperty]
            [XmlIgnore()]
            [Display(Name = "Line Color", Order = 1, GroupName = "Settings")]
            public Brush LineColor
            {
                get; set;
            }
            [Browsable(false)]
            public string LineColor_Serialize
            {
                get { return Serialize.BrushToString(this.LineColor); }
                set { this.LineColor = Serialize.StringToBrush(value); }
            }​
    Last edited by firstlanetech; 11-15-2024, 08:35 PM.

    #2
    I read under the documentation of "Creating a Custom Solid Color Brush", that the custom Solid Color Brush must be frozen using the Freeze().

    In my case using LineColor.Freeze() immediately solves the problem.

    In the OnStateChanged, in the SetDefaults section,

    Code:
    LineColor = new SolidColorBrush(Color.FromRgb(100, 125, 150));
    LineColor.Freeze();
    Is this the correct approach ? What happens if the user changes the LineColor in settings window ? Is the user specified brush frozen ?

    Comment


      #3
      Hello firstlanetech,

      Yes, this is the correct approach. The brushes are UI objects which exist on the UI thread which requires freezing to use in the NinjaScript thread.

      Below are links to the help guide.
      Help guide: NinjaScript > Educational Resources > Working with Brushes​ > Understanding custom brushes
      Help guide: NinjaScript > NinjaScript Best Practices​ > Error handling practices > Using WPF brushes

      "What happens if the user changes the LineColor in settings window ? Is the user specified brush frozen ?"

      Yes, the user selected color would be used. State.SetDefaults only sets the default value for the variable when the script is first constructed.
      Chelsea B.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      612 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      355 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      561 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      564 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X