Dynamically binding external stylesheets

4 posts, 1 answers
  1. Janitha
    Janitha avatar
    13 posts
    Member since:
    Mar 2018

    Posted 09 Oct 2018 Link to this post

    I have created a report where I have several style rules. These style rules are created on style selector and type selector. I have also several exported style rules using the styles used in the report. For example, in the report textboxes with style name 'main' have background color as yellow and in my exported style rule the background color is blue for the style name 'main'. In a console application, I am binding these external styles using the following code. 

     

    Report report = (Report)xmlSerializer.Deserialize(xmlReader);

     

    ExternalStyleSheet ess = new ExternalStyleSheet(this.themeFilePath);
    report.ExternalStyleSheets.Add(ess);

     

    The issue is even though I add the external style sheet the background color is not changing. But if I delete the style rule from the trdx/trdp file the external style sheet is applied. Is external style sheets does not override the styles that are already defined in the project? If not is there a way to override styles that are already defined in the design? 

     

    Thank you

     

  2. Todor
    Admin
    Todor avatar
    225 posts

    Posted 12 Oct 2018 Link to this post

    Hi Janitha,

    This is an expected behavior since ExternalStyleSheets have lower priority that StyleSheet. Both collections are different, hence there cannot be overriding between them.
    If you replace the inline StyleSheet with an ExternalStyleSheet, it should be successfully overridden by the ExternalStyleSheet with the same name added from the code.

    Regards,
    Todor
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Janitha
    Janitha avatar
    13 posts
    Member since:
    Mar 2018

    Posted 12 Oct 2018 in reply to Todor Link to this post

    What do you mean by  'If you replace the inline StyleSheet with an external stylesheet, it should be successfully overridden by the ExternalStyleSheet with the same name added from the code.'Is the name referred here is the style name? So for example, if an inline style rule named headerStyle is applied in the report and I can override this style by using the headerStyle.xml external style sheet?
  4. Answer
    Todor
    Admin
    Todor avatar
    225 posts

    Posted 17 Oct 2018 Link to this post

    Hello Janitha,

    ExternalStyleSheets and StyleSheet are different properties, hence the value assigned to one of them will not override the value assigned to the other. When there are two StyleRules with the same name in the ExternalStyleSheets and StyleSheet, the StyleSheet rule will be prioritized.

    If you would like to assign a StyleRule from the code that overrides a rule set in the report definition StyleSheet property, it will be necessary to add a StyleRule with the same name to the report StyleSheet collection :

    StyleRule sr = new StyleRule();
    sr.Selectors.Add(new StyleSelector("main"));
    sr.Style.BackgroundColor = System.Drawing.Color.Blue;
    report.StyleSheet.Add(sr);

    Alternatively, if you prefer to load the StyleRule 'main' from an .xml file and the rule to override a rule with the same name in the report definition, it will be necessary the StyleRule 'main' to be an item in the report definition ExternalStyleSheets collection (i.e. in report definition the rule to be loaded also from an .xml file). In this case the StyleRule applied from the code behind will override the rule from the report definition, as they will be coming from the same property (ExternalStyleSheets).

    Regards,
    Todor
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top