How to set label background with opacity from windows8palette

4 posts, 0 answers
  1. Lukasz
    Lukasz avatar
    21 posts
    Member since:
    Aug 2014

    Posted 11 Aug Link to this post

    I am trying to use AccentColor for label background with opacity:

    <telerik:Label>
        <telerik:Label.Background>
            <SolidColorBrush Color="{telerik:Windows8Resource ResourceKey=AccentBrush}" Opacity="0.1"/>
        </telerik:Label.Background>
    </telerik:Label>

    This gives me an error: "#FF25A0DA" is not a valid value for property 'Color'.

    How to get a valid color value in xaml?

  2. Martin
    Admin
    Martin avatar
    63 posts

    Posted 15 Aug Link to this post

    Hi Lukasz,

    The error is occurring because you are assigning a SolidColorBrush as a Color property of a SolidColorBrush. The brush resources of our themes (e.g. AccentBrush) are already SolidColorBrushes.

    You can achieve the desired result by getting the color value of the AccentBrush ( I could guess it is #FF25A0DA ) and change its opacity. The structure of the ColorValue is [AA][RR][GG][BB], where AA is alpha. So you can redefine the Color as #1925A0DA (as 19 is 10% of FF).

    Or in a similar way you are setting it now with Opacity as a separate property, only assign a color to the Color setter, not a SolidColorBrush reference.
    <telerik:Label>
        <telerik:Label.Background>
            <SolidColorBrush Color="#FF25A0DA" Opacity="0.1"/>
        </telerik:Label.Background>
    </telerik:Label>

    Regards,
    Martin
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Lukasz
    Lukasz avatar
    21 posts
    Member since:
    Aug 2014

    Posted 29 Aug in reply to Martin Link to this post

    Thanks for your reply. Can I somehow avoid using hardcoded value and get color value from resources? I am using Windows8 theme and I want my label to use color from theme and just add opacity to it.

    Thanks,

    Łukasz

  5. Martin
    Admin
    Martin avatar
    63 posts

    Posted 31 Aug Link to this post

    Hello Lukasz,

    You can add you the Window.Resources a custom brush, that you can define using the theme palette:

    public MainWindow()
    {
        Color win8Accent = Windows8Palette.Palette.AccentColor;
        byte opacity = (byte)((double)win8Accent.A * 0.5);
        Color MyColor = Color.FromArgb(opacity, win8Accent.R, win8Accent.G, win8Accent.B);
        var CustomBrush = new SolidColorBrush(MyColor);
     
        InitializeComponent();
     
        this.Resources.Add("CustomBrush", CustomBrush);
    }

    And refer it as a {DynamicResource CustomBrush}.

    Regards,
    Martin
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top