C1Theme: how to query e.g. C1FlexGrid colors

Posted by: wknauf on 11 August 2019, 11:53 pm EST

  • Posted 11 August 2019, 11:53 pm EST

    Hi C1,

    my use case: I build a C1FlexGrid which does not use default row alternate logic: e.g. we might have several rows for a date value, and all those rows should either have no back color or the default back color.

    This gets complicated when using C1Theme where I have to pick the alternate back color from theme.

    Attached is a sample.

    Apparently, there are two places where the C1FlexGrid alternate backcolor can be defined:
    a) "BaseThemeProperties\AltBackColor"
    b) "C1FlexGrid\Styles\Alternate\Background" - this is in my sample style ("RainerOrange") just a reference to the global "AltBackColor".

    So, I tried to query the current back color this way:
     Color colorBack = themeCurrent.GetColor("C1FlexGrid\\Styles\\Alternate\\Background");

    This will crash with an "InvalidCastException".

    My workaround:
     ThemeItem themeItem = themeCurrent.Children.GetItemByPath("C1FlexGrid\\Styles\\Alternate\\Background");
    ThemePropBase propBase = (ThemePropBase)themeItem;
    ThemeSolidBackground solidBack = (ThemeSolidBackground) propBase.Value;
    Color colorBack = solidBack.Color.Value;

    Is it a bug that "themeCurrent.GetColor("C1FlexGrid\\Styles\\Alternate\\Background");" does not work? Or should I use a different path?

    The base color can be fetched with "themeCurrent.GetColor("BaseThemeProperties\\AltBackColor");". But I don't know whether any of your default themes have a different C1FlexGrid alternate back color, so I don't know whether is is sufficient to query the back color.

    Best regards

  • Replied 12 August 2019, 10:10 pm EST


    This is the right way to fetch the background. When you query the background it is type of ThemeSolidBackground not a color type that's why InvalidCastException occurs.
    You can get the background even by using the following lines of code:
    C1.Win.C1Themes.ThemeSolidBackground  defaultColor = (C1.Win.C1Themes.ThemeSolidBackground)c1Theme1.GetEnum("C1FlexGrid\\Styles\\Alternate\\Background");

  • Replied 13 August 2019, 5:50 pm EST

    Thanks. But why does the method "GetEnum" return a class here ;-)? I would have never tried to use it because of the name and because the description also speaks about enums ;-).

  • Replied 19 August 2019, 2:55 pm EST


    I have escalated the issue to our development team(394455) and will inform you once I get any information from them.

  • Replied 7 October 2019, 11:49 pm EST


    did you receive any feedback?

    Best regards

  • Replied 8 October 2019, 9:44 pm EST

    Hello Wolfgang,

    Sorry for the delay!

    As per the development team, above mentioned way is not the correct way to get the theme background color.

    The background is not an enum. Since the theme is a regular XML file, the GetEnum method will try to parse the Value at the specified path as an enumeration (that doesn't mean it will return an enum). But since it is not an enum it just returns the whole Value. In this case, it works, because the first node of the background is Value. But in most cases this line should throw exception.

    Background is a complicated item. It can be an instance of different classes: ThemeSolidBackground, ThemeGradientBackground. And even instances of the same class can be different in structure: LinearGradient, TwoColorGradient, MultiColorGradient, RadialGradient and so on.

    But if you just needs to get a solid color of a background, then the right way to do this would be:
     Color backColor = Color.Empty;
    ThemePropBase themeItem = theme.Children.GetItemByPath("C1FlexGrid\\Styles\\Alternate\\Background") as ThemePropBase;
    // item can be null in some theme, this will means that the control should use the default color
    if(themeItem != null)
    // get base class for all Backgrounds
    ThemeBackground background = themeItem.Value as ThemeBackground;
    // This should never happen, but since we used 'as' it would be more correct to use a null-check here
    if (background != null)
    // try to get a solid color
    Color? color = background.GetSolidColor();
    // In some cases, it may be null or Empty (for example, some gradient backgrounds)
    if (color.HasValue)
    backColor = color.Value;

    Also, you can also view the XML structure of any theme if you want:
    "C:\Program Files (x86)\ComponentOne\WinForms Edition\C1Themes\Themes\<themename>"
    Then, perhaps, much will become clearer.

    Hope it clarifies.


  • Replied 9 October 2019, 6:24 pm EST

    Hi Mohit,

    thanks for the clarification. Your suggestion is basically the same as my initial resolution. So I simply forget about your suggestion to use "GetEnum" ;-).

    Your answer also explains why "theme.GetColor" does not work here, as the "Background" color might be a gradient (with two color values?).

    Best regards

  • Replied 10 October 2019, 9:46 pm EST

    Hello Wolfgang!

    Yes and I am very sorry for the confusion.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels