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

how to retrieve this xaml value via code?

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

    how to retrieve this xaml value via code?

    Hi, I've run into a wall here ...

    I'm trying to get some color values from the BluePrint.xaml file, I'm at a point where the file says:
    Code:
        <LinearGradientBrush    po:Freeze="true"    x:Key="TabItemNormalBackground"        StartPoint="0.5,0" EndPoint="0.5,1">
    
            <LinearGradientBrush.GradientStops>
                <GradientStop Color="#403f45" Offset="0"/>
                <GradientStop Color="#403f45" Offset="0.3"/>
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
    I'm trying to get the values of "GradientStop Color"
    I've tried using:
    Code:
    LinearGradientBrush TabItemNormalBackground    = (LinearGradientBrush)Application.Current.Resources["TabItemNormalBackground"];
    and:
    Code:
    SolidColorBrush TabItemNormalBackground        = (SolidColorBrush)Application.Current.Resources["TabItemNormalBackground/LinearGradientBrush.GradientStops/GradientStop"];
    but they both seem to get the same incorrect result:
    Code:
                      <LinearGradientBrush StartPoint="0.5,1" EndPoint="0.5,0.2">
                        <LinearGradientBrush.GradientStops>
                          <GradientStop Color="#FFFFFFFF" Offset="0" />
                          <GradientStop Color="#FFFFFFFF" Offset="1" />
                        </LinearGradientBrush.GradientStops>
                      </LinearGradientBrush>
    is there a way to correctly get it down to just the gradientstop values?


    thanks!
    Last edited by gubbar924; 01-23-2017, 03:28 PM.

    #2
    Thank you for your question gubbar924. While we can provide limited WPF support, when I reviewed MSDN C#'s publicly available documentation here,

    Learn how to use objects to paint with solid colors, linear gradients, and radial gradients in Windows Presentation Foundation (WPF).


    the documented code examples they provide all instantiate gradients programmatically, e.g.

    Code:
    [FONT=Courier New]
    // Create a diagonal linear gradient with four stops.   
    LinearGradientBrush myLinearGradientBrush =
        new LinearGradientBrush();
    myLinearGradientBrush.StartPoint = new Point(0,0);
    myLinearGradientBrush.EndPoint = new Point(1,1);
    myLinearGradientBrush.GradientStops.Add(
        new GradientStop(Colors.Yellow, 0.0));
    myLinearGradientBrush.GradientStops.Add(
        new GradientStop(Colors.Red, 0.25));                
    myLinearGradientBrush.GradientStops.Add(
        new GradientStop(Colors.Blue, 0.75));        
    myLinearGradientBrush.GradientStops.Add(
        new GradientStop(Colors.LimeGreen, 1.0));[/FONT]
    This implies that, should you want to define a gradient through xaml, it may be best to define the stop points in UI strings that are accessible both by xaml and by your C# code, and then to create a new gradient as detailed above using these referenced values, as this would be closer to the approach that MSDN supports directly in the above documentation. This publicly available documentation can guide you through pulling in UI strings,



    Please let us know if there are any other ways we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hi Jessica,

      What I'm trying to do is retrieve this info (from xaml), not set them.

      What I had been doing up till now was something like this:
      SolidColorBrush xxx = (SolidColorBrush)Application.Current.Resources["xxx"];

      for simple 1 liners such as:
      <SolidColorBrush po:Freeze="true" x:Key="RadioButtonCheckMark" Color="#FFFFFFFF"/>

      and this would correctly set xxx as #FFFFFFFF
      but now that TabItemNormalBackground is not so simple, I wasnt quite sure how to get to the colors & start/end points. I think I have it figured out now though.


      Thanks again for the quick reply!

      Comment


        #4
        I am glad you were able to resolve your query. While I understand your time is valuable, for the benefit of other customers trying to retrieve gradients from XAML, if it is possible for you to provide a working code sample it would be appreciated.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          No problem!


          For this example, lets assume that the currently applied skin is "Slate Gray", so the info that we retrieve is in the file:
          %USERPROFILE%\Documents\NinjaTrader 8\templates\Skins\Slate Gray\BluePrint.xaml
          Within this file, lines 175-182 read as:
          Code:
              <system:Double    x:Key="SizeLinkControlMenu">14</system:Double>
          
              <LinearGradientBrush po:Freeze="true" x:Key="LinkAllGreenBackground" StartPoint="0.5,-0.2" EndPoint="0.5,0.62">
                  <LinearGradientBrush.GradientStops>
                      <GradientStop Color="#FFFAFBFC" Offset="0"/>
                      <GradientStop Color="#FF00FF00" Offset="0.75"/>
                  </LinearGradientBrush.GradientStops>
              </LinearGradientBrush>
          if you want to pull the value of just a single x:Key,
          you simply follow the basic template:
          Code:
          varType varName = (varType)Application.Current.Resources["x:Key"];
          since "SizeLinkControlMenu" is a double
          Code:
          double SizeLinkControlMenuA  = (double)Application.Current.Resources["SizeLinkControlMenu"];
          you can also retrieve it as a string
          Code:
          string SizeLinkControlMenuB  = Application.Current.Resources["SizeLinkControlMenu"].ToString();
          however, this did not work for multi-line properties such as the above listed linear gradient brush.
          if you try
          Code:
          LinearGradientBrush LinkAllGreenBackground = (LinearGradientBrush)Application.Current.Resources["LinkAllGreenBackground"];
          Print(LinkAllGreenBackground);
          OR
          Code:
          string LinkAllGreenBackground = Application.Current.Resources["LinkAllGreenBackground"].ToString();
          Print(LinkAllGreenBackground);
          you will simply get
          System.Windows.Media.LinearGradientBrush
          in the output window, rather than a list of its properties.

          In order to access the start/end points, the gradient stop colors, and their offsets, we have to add some extra steps.

          Code:
                      else if (State == State.DataLoaded)
                      {
                          double SizeLinkControlMenuA                   = (double)Application.Current.Resources["SizeLinkControlMenu"];
                          string SizeLinkControlMenuB                   = Application.Current.Resources["SizeLinkControlMenu"].ToString();
                          LinearGradientBrush LinkAllGreenBackground = (LinearGradientBrush)Application.Current.Resources["LinkAllGreenBackground"];
                          Point startPoint                           = LinkAllGreenBackground.StartPoint;
                          Point endPoint                               = LinkAllGreenBackground.EndPoint;
                          Color GradientStop1                           = LinkAllGreenBackground.GradientStops[0].Color;
                          Color GradientStop2                           = LinkAllGreenBackground.GradientStops[1].Color;
                          double offset1                               = LinkAllGreenBackground.GradientStops[0].Offset;
                          double offset2                               = LinkAllGreenBackground.GradientStops[1].Offset;
                          Print(SizeLinkControlMenuA);
                          Print(SizeLinkControlMenuB);
                          Print(startPoint);
                          Print(endPoint);
                          Print(GradientStop1);
                          Print(GradientStop2);
                          Print(offset1);
                          Print(offset2);
                      }
          will correctly print
          14
          14
          0.5,-0.2
          0.5,0.62
          #FFFAFBFC
          #FF00FF00
          0
          0.75
          OR
          a simpler way to retrieve a single value
          Code:
          string GradientStop1 = (LinearGradientBrush)Application.Current.Resources["LinkAllGreenBackground"].GradientStops[0].Color.ToString();
          Because GradientStops is a collection, we access 1st, 2nd, 3rd, etc values via their index number.
          So the first gradient stop color listed = GradientStops[0].Color.

          I hope this saves you guys some time. I'm just a beginner, and tried to write this as if another beginner was reading it. I wouldn't be surprised if there are better ways of going about this, so if you have one, please do share. Thanks guys!
          Last edited by gubbar924; 01-23-2017, 03:05 PM.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Mongo, Today, 11:05 AM
          4 responses
          14 views
          0 likes
          Last Post Mongo
          by Mongo
           
          Started by traderqz, Today, 12:06 AM
          7 responses
          13 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by Skifree, Today, 03:41 AM
          5 responses
          13 views
          0 likes
          Last Post Skifree
          by Skifree
           
          Started by traderqz, Yesterday, 09:06 AM
          5 responses
          34 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by guillembm, Today, 11:25 AM
          1 response
          6 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Working...
          X