Setting WPF background Opacity programmatically





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I'm working on application which creates a new wpf border component for each row in a database. This means I've got to style the border component in C# rather than XAML (as far as I'm aware). The styling is all good so far apart from trying to set the background opacity.



motherboards.Add(new Border());
Border moboBorder = motherboards[i];
moboBorder.Width = 150;
moboBorder.Height = 150;
moboBorder.BorderBrush = Brushes.Black;
moboBorder.Background = Brushes.White;
moboBorder.CornerRadius = new CornerRadius(10);
moboBorder.Margin = new Thickness(5);
moboBorder.BorderThickness = new Thickness(1);


You can adjust the background opacity in XAML like so



<Border BorderThickness="1" Height="100" Width="100">
<Border.BorderBrush>
<SolidColorBrush Color="Black" Opacity="0.7"/>
</Border.BorderBrush>
</Border>


But as I've said I'm creating the component in C# rather than XAML. I guess this is how you set the value in c#



moboBorder.Background.Opacity = //Value


However, I can't figure out what kind of value it takes, not just a straight up number, nothing from brushes than I can see and nothing like = new Opacity()



I've tried googling around but everything is about setting the opacity for the whole element rather than just the background of it.










share|improve this question

























  • Doesn't it take a double, between 0.0 and 1.0?

    – Geoff James
    Dec 4 '17 at 20:50













  • @GeoffJames that's what I thought but no it says that it's read only: i.imgur.com/VEkaW9I.png

    – nomicron
    Dec 4 '17 at 20:52








  • 2





    @nomicron You can't set the opacity property on a brush once it's in use. You can create a new Brush though: Background = new SolidColorBrush(((SolidColorBrush)Background).Color) { Opacity = 0.5 };

    – Ed Plunkett
    Dec 4 '17 at 20:54











  • So you can't set it because it's readonly. That's because you're trying to set the Opacity of a Brush. You need to set the Background` itself with a Brush with an Opacity of your desire

    – Geoff James
    Dec 4 '17 at 20:54











  • For any future reader (and potential comment upvoter), please note that it's wrong to say that you "can't set the opacity property on a brush once it's in use". You can very well do that, provided that the Brush is not frozen. The members of the Brushes class are frozen, and can therefore not be changed (regardless whether they are in use or not).

    – Clemens
    Dec 5 '17 at 10:16




















2















I'm working on application which creates a new wpf border component for each row in a database. This means I've got to style the border component in C# rather than XAML (as far as I'm aware). The styling is all good so far apart from trying to set the background opacity.



motherboards.Add(new Border());
Border moboBorder = motherboards[i];
moboBorder.Width = 150;
moboBorder.Height = 150;
moboBorder.BorderBrush = Brushes.Black;
moboBorder.Background = Brushes.White;
moboBorder.CornerRadius = new CornerRadius(10);
moboBorder.Margin = new Thickness(5);
moboBorder.BorderThickness = new Thickness(1);


You can adjust the background opacity in XAML like so



<Border BorderThickness="1" Height="100" Width="100">
<Border.BorderBrush>
<SolidColorBrush Color="Black" Opacity="0.7"/>
</Border.BorderBrush>
</Border>


But as I've said I'm creating the component in C# rather than XAML. I guess this is how you set the value in c#



moboBorder.Background.Opacity = //Value


However, I can't figure out what kind of value it takes, not just a straight up number, nothing from brushes than I can see and nothing like = new Opacity()



I've tried googling around but everything is about setting the opacity for the whole element rather than just the background of it.










share|improve this question

























  • Doesn't it take a double, between 0.0 and 1.0?

    – Geoff James
    Dec 4 '17 at 20:50













  • @GeoffJames that's what I thought but no it says that it's read only: i.imgur.com/VEkaW9I.png

    – nomicron
    Dec 4 '17 at 20:52








  • 2





    @nomicron You can't set the opacity property on a brush once it's in use. You can create a new Brush though: Background = new SolidColorBrush(((SolidColorBrush)Background).Color) { Opacity = 0.5 };

    – Ed Plunkett
    Dec 4 '17 at 20:54











  • So you can't set it because it's readonly. That's because you're trying to set the Opacity of a Brush. You need to set the Background` itself with a Brush with an Opacity of your desire

    – Geoff James
    Dec 4 '17 at 20:54











  • For any future reader (and potential comment upvoter), please note that it's wrong to say that you "can't set the opacity property on a brush once it's in use". You can very well do that, provided that the Brush is not frozen. The members of the Brushes class are frozen, and can therefore not be changed (regardless whether they are in use or not).

    – Clemens
    Dec 5 '17 at 10:16
















2












2








2


1






I'm working on application which creates a new wpf border component for each row in a database. This means I've got to style the border component in C# rather than XAML (as far as I'm aware). The styling is all good so far apart from trying to set the background opacity.



motherboards.Add(new Border());
Border moboBorder = motherboards[i];
moboBorder.Width = 150;
moboBorder.Height = 150;
moboBorder.BorderBrush = Brushes.Black;
moboBorder.Background = Brushes.White;
moboBorder.CornerRadius = new CornerRadius(10);
moboBorder.Margin = new Thickness(5);
moboBorder.BorderThickness = new Thickness(1);


You can adjust the background opacity in XAML like so



<Border BorderThickness="1" Height="100" Width="100">
<Border.BorderBrush>
<SolidColorBrush Color="Black" Opacity="0.7"/>
</Border.BorderBrush>
</Border>


But as I've said I'm creating the component in C# rather than XAML. I guess this is how you set the value in c#



moboBorder.Background.Opacity = //Value


However, I can't figure out what kind of value it takes, not just a straight up number, nothing from brushes than I can see and nothing like = new Opacity()



I've tried googling around but everything is about setting the opacity for the whole element rather than just the background of it.










share|improve this question
















I'm working on application which creates a new wpf border component for each row in a database. This means I've got to style the border component in C# rather than XAML (as far as I'm aware). The styling is all good so far apart from trying to set the background opacity.



motherboards.Add(new Border());
Border moboBorder = motherboards[i];
moboBorder.Width = 150;
moboBorder.Height = 150;
moboBorder.BorderBrush = Brushes.Black;
moboBorder.Background = Brushes.White;
moboBorder.CornerRadius = new CornerRadius(10);
moboBorder.Margin = new Thickness(5);
moboBorder.BorderThickness = new Thickness(1);


You can adjust the background opacity in XAML like so



<Border BorderThickness="1" Height="100" Width="100">
<Border.BorderBrush>
<SolidColorBrush Color="Black" Opacity="0.7"/>
</Border.BorderBrush>
</Border>


But as I've said I'm creating the component in C# rather than XAML. I guess this is how you set the value in c#



moboBorder.Background.Opacity = //Value


However, I can't figure out what kind of value it takes, not just a straight up number, nothing from brushes than I can see and nothing like = new Opacity()



I've tried googling around but everything is about setting the opacity for the whole element rather than just the background of it.







c# wpf opacity






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 5:56









Cœur

19.3k10116155




19.3k10116155










asked Dec 4 '17 at 20:43









nomicronnomicron

3210




3210













  • Doesn't it take a double, between 0.0 and 1.0?

    – Geoff James
    Dec 4 '17 at 20:50













  • @GeoffJames that's what I thought but no it says that it's read only: i.imgur.com/VEkaW9I.png

    – nomicron
    Dec 4 '17 at 20:52








  • 2





    @nomicron You can't set the opacity property on a brush once it's in use. You can create a new Brush though: Background = new SolidColorBrush(((SolidColorBrush)Background).Color) { Opacity = 0.5 };

    – Ed Plunkett
    Dec 4 '17 at 20:54











  • So you can't set it because it's readonly. That's because you're trying to set the Opacity of a Brush. You need to set the Background` itself with a Brush with an Opacity of your desire

    – Geoff James
    Dec 4 '17 at 20:54











  • For any future reader (and potential comment upvoter), please note that it's wrong to say that you "can't set the opacity property on a brush once it's in use". You can very well do that, provided that the Brush is not frozen. The members of the Brushes class are frozen, and can therefore not be changed (regardless whether they are in use or not).

    – Clemens
    Dec 5 '17 at 10:16





















  • Doesn't it take a double, between 0.0 and 1.0?

    – Geoff James
    Dec 4 '17 at 20:50













  • @GeoffJames that's what I thought but no it says that it's read only: i.imgur.com/VEkaW9I.png

    – nomicron
    Dec 4 '17 at 20:52








  • 2





    @nomicron You can't set the opacity property on a brush once it's in use. You can create a new Brush though: Background = new SolidColorBrush(((SolidColorBrush)Background).Color) { Opacity = 0.5 };

    – Ed Plunkett
    Dec 4 '17 at 20:54











  • So you can't set it because it's readonly. That's because you're trying to set the Opacity of a Brush. You need to set the Background` itself with a Brush with an Opacity of your desire

    – Geoff James
    Dec 4 '17 at 20:54











  • For any future reader (and potential comment upvoter), please note that it's wrong to say that you "can't set the opacity property on a brush once it's in use". You can very well do that, provided that the Brush is not frozen. The members of the Brushes class are frozen, and can therefore not be changed (regardless whether they are in use or not).

    – Clemens
    Dec 5 '17 at 10:16



















Doesn't it take a double, between 0.0 and 1.0?

– Geoff James
Dec 4 '17 at 20:50







Doesn't it take a double, between 0.0 and 1.0?

– Geoff James
Dec 4 '17 at 20:50















@GeoffJames that's what I thought but no it says that it's read only: i.imgur.com/VEkaW9I.png

– nomicron
Dec 4 '17 at 20:52







@GeoffJames that's what I thought but no it says that it's read only: i.imgur.com/VEkaW9I.png

– nomicron
Dec 4 '17 at 20:52






2




2





@nomicron You can't set the opacity property on a brush once it's in use. You can create a new Brush though: Background = new SolidColorBrush(((SolidColorBrush)Background).Color) { Opacity = 0.5 };

– Ed Plunkett
Dec 4 '17 at 20:54





@nomicron You can't set the opacity property on a brush once it's in use. You can create a new Brush though: Background = new SolidColorBrush(((SolidColorBrush)Background).Color) { Opacity = 0.5 };

– Ed Plunkett
Dec 4 '17 at 20:54













So you can't set it because it's readonly. That's because you're trying to set the Opacity of a Brush. You need to set the Background` itself with a Brush with an Opacity of your desire

– Geoff James
Dec 4 '17 at 20:54





So you can't set it because it's readonly. That's because you're trying to set the Opacity of a Brush. You need to set the Background` itself with a Brush with an Opacity of your desire

– Geoff James
Dec 4 '17 at 20:54













For any future reader (and potential comment upvoter), please note that it's wrong to say that you "can't set the opacity property on a brush once it's in use". You can very well do that, provided that the Brush is not frozen. The members of the Brushes class are frozen, and can therefore not be changed (regardless whether they are in use or not).

– Clemens
Dec 5 '17 at 10:16







For any future reader (and potential comment upvoter), please note that it's wrong to say that you "can't set the opacity property on a brush once it's in use". You can very well do that, provided that the Brush is not frozen. The members of the Brushes class are frozen, and can therefore not be changed (regardless whether they are in use or not).

– Clemens
Dec 5 '17 at 10:16














3 Answers
3






active

oldest

votes


















6














A double is certainly a "straight up number"; hover the mouse over the property to see the data type.



The problem (thanks, Clemens) is that you're trying to set the opacity of Brushes.Black, which is a system object and you've got no business doing that.



But you can set the Opacity of a SolidColorBrush that you create yourself.



To create a new semi-opaque white brush:



x.Background = new SolidColorBrush(Colors.White) { Opacity = 0.5 };


See Geoff's answer for how to create a color from an RGB triplet (or ARGB quad) instead of named colors.



Or just keep the existing brush, if you're confident that you didn't get it from Brushes.



Background.Opacity = 0.5;


If you did this, you got it from System.Brushes:



<Window
Background="DeepSkyBlue"
...


If you did this, you didn't:



<Window.Background><SolidColorBrush Color="DeepSkyBlue" /></Window.Background>


That DeepSkyBlue is Colors.DeepSkyBlue; you're creating a new brush with that color.



You should be doing all of this in XAML with bindings instead of creating WPF controls in C#. You'll shoot your eye out, kid.



But it's your eye.






share|improve this answer





















  • 1





    Totally agree with your suggestion of using bindings. MVVM all the way :o)

    – Geoff James
    Dec 4 '17 at 20:59











  • Exactly what I needed, great thank you

    – nomicron
    Dec 4 '17 at 21:00






  • 1





    It's not true that "you can't set the Opacity of a Brush once it's in use". You just can't change the Opacity of the members of the Brushes class (as these are readonly). However, if you assign Background = new SolidColorBrush(Colors.Black) to an element, you can easily later set Background.Opacity = 0.5. Both answers should be edited.

    – Clemens
    Dec 4 '17 at 22:24













  • @Clemens Whoops, thanks. Updated.

    – Ed Plunkett
    Dec 4 '17 at 22:30






  • 2





    @Clemens - I've probably taken your comment out of context and been a bit subjective, sorry. Far too often I see members of SO trolling all over others' answers for "see my answer" comments. I agree it's very constructive and adds good value to point these things out. I've deleted my stupid-ass comment, now, so as not to draw away from the point of the answers.

    – Geoff James
    Dec 4 '17 at 22:56





















2














As @Clemens kindly pointed out in the comments:



You can't set the Opacity of the system's shared brushes directly.



You will need to use a non-shared SolidColorBrush, and then you will be able to set the Opacity of that.



You will be able to change the Opacity from any point in the code, from thereon-in.



E.g.:



moboBorder.Background = new SolidColorBrush(Color.FromRgb(255, 0, 0))
{
Opacity = 0.5 // or whatever opacity between
// 0.0 (0%) and 1.0 (100%)
};


Or



moboBorder.Background = new SolidColorBrush(Color.FromRgb(255, 0, 0));
moboBorder.Background.Opacity = 0.5;




Similar the above example, you could also set the alpha (the opacity) if you're using RGB.



You can use the Color.FromArgb() static method, instead:



moboBorder.Background = new SolidColorBrush(Color.FromArgb(0.5, 255, 0, 0));


Just use a double between 0.0 and 1.0 (as before), as your first argument to the method.





Hope this helps.






share|improve this answer





















  • 1





    It's not true that "you can't set the Opacity of a Brush once it's in use". You just can't change the Opacity of the members of the Brushes class (as these are readonly). However, if you assign Background = new SolidColorBrush(Colors.Black) to an element, you can easily later set Background.Opacity = 0.5. Both answers should be edited.

    – Clemens
    Dec 4 '17 at 22:26













  • Good point, @Clemens - absolutely right! Updated my answer to clarify this to do with the shared brushes. Thanks!

    – Geoff James
    Dec 4 '17 at 22:32



















2














The equivalent of the XAML



<Border.BorderBrush>
<SolidColorBrush Color="Black" Opacity="0.7"/>
</Border.BorderBrush>


in code behind would be



moboBorder.Background = new SolidColorBrush
{
Color = Colors.Black,
Opacity = 0.7
};


In contrast to the predefined Brushes in the Brushes class (which are frozen), the above SolidColorBrush can be changed at any time later, like



moboBorder.Background.Opacity = 0.5;





share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47641658%2fsetting-wpf-background-opacity-programmatically%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    A double is certainly a "straight up number"; hover the mouse over the property to see the data type.



    The problem (thanks, Clemens) is that you're trying to set the opacity of Brushes.Black, which is a system object and you've got no business doing that.



    But you can set the Opacity of a SolidColorBrush that you create yourself.



    To create a new semi-opaque white brush:



    x.Background = new SolidColorBrush(Colors.White) { Opacity = 0.5 };


    See Geoff's answer for how to create a color from an RGB triplet (or ARGB quad) instead of named colors.



    Or just keep the existing brush, if you're confident that you didn't get it from Brushes.



    Background.Opacity = 0.5;


    If you did this, you got it from System.Brushes:



    <Window
    Background="DeepSkyBlue"
    ...


    If you did this, you didn't:



    <Window.Background><SolidColorBrush Color="DeepSkyBlue" /></Window.Background>


    That DeepSkyBlue is Colors.DeepSkyBlue; you're creating a new brush with that color.



    You should be doing all of this in XAML with bindings instead of creating WPF controls in C#. You'll shoot your eye out, kid.



    But it's your eye.






    share|improve this answer





















    • 1





      Totally agree with your suggestion of using bindings. MVVM all the way :o)

      – Geoff James
      Dec 4 '17 at 20:59











    • Exactly what I needed, great thank you

      – nomicron
      Dec 4 '17 at 21:00






    • 1





      It's not true that "you can't set the Opacity of a Brush once it's in use". You just can't change the Opacity of the members of the Brushes class (as these are readonly). However, if you assign Background = new SolidColorBrush(Colors.Black) to an element, you can easily later set Background.Opacity = 0.5. Both answers should be edited.

      – Clemens
      Dec 4 '17 at 22:24













    • @Clemens Whoops, thanks. Updated.

      – Ed Plunkett
      Dec 4 '17 at 22:30






    • 2





      @Clemens - I've probably taken your comment out of context and been a bit subjective, sorry. Far too often I see members of SO trolling all over others' answers for "see my answer" comments. I agree it's very constructive and adds good value to point these things out. I've deleted my stupid-ass comment, now, so as not to draw away from the point of the answers.

      – Geoff James
      Dec 4 '17 at 22:56


















    6














    A double is certainly a "straight up number"; hover the mouse over the property to see the data type.



    The problem (thanks, Clemens) is that you're trying to set the opacity of Brushes.Black, which is a system object and you've got no business doing that.



    But you can set the Opacity of a SolidColorBrush that you create yourself.



    To create a new semi-opaque white brush:



    x.Background = new SolidColorBrush(Colors.White) { Opacity = 0.5 };


    See Geoff's answer for how to create a color from an RGB triplet (or ARGB quad) instead of named colors.



    Or just keep the existing brush, if you're confident that you didn't get it from Brushes.



    Background.Opacity = 0.5;


    If you did this, you got it from System.Brushes:



    <Window
    Background="DeepSkyBlue"
    ...


    If you did this, you didn't:



    <Window.Background><SolidColorBrush Color="DeepSkyBlue" /></Window.Background>


    That DeepSkyBlue is Colors.DeepSkyBlue; you're creating a new brush with that color.



    You should be doing all of this in XAML with bindings instead of creating WPF controls in C#. You'll shoot your eye out, kid.



    But it's your eye.






    share|improve this answer





















    • 1





      Totally agree with your suggestion of using bindings. MVVM all the way :o)

      – Geoff James
      Dec 4 '17 at 20:59











    • Exactly what I needed, great thank you

      – nomicron
      Dec 4 '17 at 21:00






    • 1





      It's not true that "you can't set the Opacity of a Brush once it's in use". You just can't change the Opacity of the members of the Brushes class (as these are readonly). However, if you assign Background = new SolidColorBrush(Colors.Black) to an element, you can easily later set Background.Opacity = 0.5. Both answers should be edited.

      – Clemens
      Dec 4 '17 at 22:24













    • @Clemens Whoops, thanks. Updated.

      – Ed Plunkett
      Dec 4 '17 at 22:30






    • 2





      @Clemens - I've probably taken your comment out of context and been a bit subjective, sorry. Far too often I see members of SO trolling all over others' answers for "see my answer" comments. I agree it's very constructive and adds good value to point these things out. I've deleted my stupid-ass comment, now, so as not to draw away from the point of the answers.

      – Geoff James
      Dec 4 '17 at 22:56
















    6












    6








    6







    A double is certainly a "straight up number"; hover the mouse over the property to see the data type.



    The problem (thanks, Clemens) is that you're trying to set the opacity of Brushes.Black, which is a system object and you've got no business doing that.



    But you can set the Opacity of a SolidColorBrush that you create yourself.



    To create a new semi-opaque white brush:



    x.Background = new SolidColorBrush(Colors.White) { Opacity = 0.5 };


    See Geoff's answer for how to create a color from an RGB triplet (or ARGB quad) instead of named colors.



    Or just keep the existing brush, if you're confident that you didn't get it from Brushes.



    Background.Opacity = 0.5;


    If you did this, you got it from System.Brushes:



    <Window
    Background="DeepSkyBlue"
    ...


    If you did this, you didn't:



    <Window.Background><SolidColorBrush Color="DeepSkyBlue" /></Window.Background>


    That DeepSkyBlue is Colors.DeepSkyBlue; you're creating a new brush with that color.



    You should be doing all of this in XAML with bindings instead of creating WPF controls in C#. You'll shoot your eye out, kid.



    But it's your eye.






    share|improve this answer















    A double is certainly a "straight up number"; hover the mouse over the property to see the data type.



    The problem (thanks, Clemens) is that you're trying to set the opacity of Brushes.Black, which is a system object and you've got no business doing that.



    But you can set the Opacity of a SolidColorBrush that you create yourself.



    To create a new semi-opaque white brush:



    x.Background = new SolidColorBrush(Colors.White) { Opacity = 0.5 };


    See Geoff's answer for how to create a color from an RGB triplet (or ARGB quad) instead of named colors.



    Or just keep the existing brush, if you're confident that you didn't get it from Brushes.



    Background.Opacity = 0.5;


    If you did this, you got it from System.Brushes:



    <Window
    Background="DeepSkyBlue"
    ...


    If you did this, you didn't:



    <Window.Background><SolidColorBrush Color="DeepSkyBlue" /></Window.Background>


    That DeepSkyBlue is Colors.DeepSkyBlue; you're creating a new brush with that color.



    You should be doing all of this in XAML with bindings instead of creating WPF controls in C#. You'll shoot your eye out, kid.



    But it's your eye.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 4 '17 at 22:53

























    answered Dec 4 '17 at 20:58









    Ed PlunkettEd Plunkett

    30.2k53679




    30.2k53679








    • 1





      Totally agree with your suggestion of using bindings. MVVM all the way :o)

      – Geoff James
      Dec 4 '17 at 20:59











    • Exactly what I needed, great thank you

      – nomicron
      Dec 4 '17 at 21:00






    • 1





      It's not true that "you can't set the Opacity of a Brush once it's in use". You just can't change the Opacity of the members of the Brushes class (as these are readonly). However, if you assign Background = new SolidColorBrush(Colors.Black) to an element, you can easily later set Background.Opacity = 0.5. Both answers should be edited.

      – Clemens
      Dec 4 '17 at 22:24













    • @Clemens Whoops, thanks. Updated.

      – Ed Plunkett
      Dec 4 '17 at 22:30






    • 2





      @Clemens - I've probably taken your comment out of context and been a bit subjective, sorry. Far too often I see members of SO trolling all over others' answers for "see my answer" comments. I agree it's very constructive and adds good value to point these things out. I've deleted my stupid-ass comment, now, so as not to draw away from the point of the answers.

      – Geoff James
      Dec 4 '17 at 22:56
















    • 1





      Totally agree with your suggestion of using bindings. MVVM all the way :o)

      – Geoff James
      Dec 4 '17 at 20:59











    • Exactly what I needed, great thank you

      – nomicron
      Dec 4 '17 at 21:00






    • 1





      It's not true that "you can't set the Opacity of a Brush once it's in use". You just can't change the Opacity of the members of the Brushes class (as these are readonly). However, if you assign Background = new SolidColorBrush(Colors.Black) to an element, you can easily later set Background.Opacity = 0.5. Both answers should be edited.

      – Clemens
      Dec 4 '17 at 22:24













    • @Clemens Whoops, thanks. Updated.

      – Ed Plunkett
      Dec 4 '17 at 22:30






    • 2





      @Clemens - I've probably taken your comment out of context and been a bit subjective, sorry. Far too often I see members of SO trolling all over others' answers for "see my answer" comments. I agree it's very constructive and adds good value to point these things out. I've deleted my stupid-ass comment, now, so as not to draw away from the point of the answers.

      – Geoff James
      Dec 4 '17 at 22:56










    1




    1





    Totally agree with your suggestion of using bindings. MVVM all the way :o)

    – Geoff James
    Dec 4 '17 at 20:59





    Totally agree with your suggestion of using bindings. MVVM all the way :o)

    – Geoff James
    Dec 4 '17 at 20:59













    Exactly what I needed, great thank you

    – nomicron
    Dec 4 '17 at 21:00





    Exactly what I needed, great thank you

    – nomicron
    Dec 4 '17 at 21:00




    1




    1





    It's not true that "you can't set the Opacity of a Brush once it's in use". You just can't change the Opacity of the members of the Brushes class (as these are readonly). However, if you assign Background = new SolidColorBrush(Colors.Black) to an element, you can easily later set Background.Opacity = 0.5. Both answers should be edited.

    – Clemens
    Dec 4 '17 at 22:24







    It's not true that "you can't set the Opacity of a Brush once it's in use". You just can't change the Opacity of the members of the Brushes class (as these are readonly). However, if you assign Background = new SolidColorBrush(Colors.Black) to an element, you can easily later set Background.Opacity = 0.5. Both answers should be edited.

    – Clemens
    Dec 4 '17 at 22:24















    @Clemens Whoops, thanks. Updated.

    – Ed Plunkett
    Dec 4 '17 at 22:30





    @Clemens Whoops, thanks. Updated.

    – Ed Plunkett
    Dec 4 '17 at 22:30




    2




    2





    @Clemens - I've probably taken your comment out of context and been a bit subjective, sorry. Far too often I see members of SO trolling all over others' answers for "see my answer" comments. I agree it's very constructive and adds good value to point these things out. I've deleted my stupid-ass comment, now, so as not to draw away from the point of the answers.

    – Geoff James
    Dec 4 '17 at 22:56







    @Clemens - I've probably taken your comment out of context and been a bit subjective, sorry. Far too often I see members of SO trolling all over others' answers for "see my answer" comments. I agree it's very constructive and adds good value to point these things out. I've deleted my stupid-ass comment, now, so as not to draw away from the point of the answers.

    – Geoff James
    Dec 4 '17 at 22:56















    2














    As @Clemens kindly pointed out in the comments:



    You can't set the Opacity of the system's shared brushes directly.



    You will need to use a non-shared SolidColorBrush, and then you will be able to set the Opacity of that.



    You will be able to change the Opacity from any point in the code, from thereon-in.



    E.g.:



    moboBorder.Background = new SolidColorBrush(Color.FromRgb(255, 0, 0))
    {
    Opacity = 0.5 // or whatever opacity between
    // 0.0 (0%) and 1.0 (100%)
    };


    Or



    moboBorder.Background = new SolidColorBrush(Color.FromRgb(255, 0, 0));
    moboBorder.Background.Opacity = 0.5;




    Similar the above example, you could also set the alpha (the opacity) if you're using RGB.



    You can use the Color.FromArgb() static method, instead:



    moboBorder.Background = new SolidColorBrush(Color.FromArgb(0.5, 255, 0, 0));


    Just use a double between 0.0 and 1.0 (as before), as your first argument to the method.





    Hope this helps.






    share|improve this answer





















    • 1





      It's not true that "you can't set the Opacity of a Brush once it's in use". You just can't change the Opacity of the members of the Brushes class (as these are readonly). However, if you assign Background = new SolidColorBrush(Colors.Black) to an element, you can easily later set Background.Opacity = 0.5. Both answers should be edited.

      – Clemens
      Dec 4 '17 at 22:26













    • Good point, @Clemens - absolutely right! Updated my answer to clarify this to do with the shared brushes. Thanks!

      – Geoff James
      Dec 4 '17 at 22:32
















    2














    As @Clemens kindly pointed out in the comments:



    You can't set the Opacity of the system's shared brushes directly.



    You will need to use a non-shared SolidColorBrush, and then you will be able to set the Opacity of that.



    You will be able to change the Opacity from any point in the code, from thereon-in.



    E.g.:



    moboBorder.Background = new SolidColorBrush(Color.FromRgb(255, 0, 0))
    {
    Opacity = 0.5 // or whatever opacity between
    // 0.0 (0%) and 1.0 (100%)
    };


    Or



    moboBorder.Background = new SolidColorBrush(Color.FromRgb(255, 0, 0));
    moboBorder.Background.Opacity = 0.5;




    Similar the above example, you could also set the alpha (the opacity) if you're using RGB.



    You can use the Color.FromArgb() static method, instead:



    moboBorder.Background = new SolidColorBrush(Color.FromArgb(0.5, 255, 0, 0));


    Just use a double between 0.0 and 1.0 (as before), as your first argument to the method.





    Hope this helps.






    share|improve this answer





















    • 1





      It's not true that "you can't set the Opacity of a Brush once it's in use". You just can't change the Opacity of the members of the Brushes class (as these are readonly). However, if you assign Background = new SolidColorBrush(Colors.Black) to an element, you can easily later set Background.Opacity = 0.5. Both answers should be edited.

      – Clemens
      Dec 4 '17 at 22:26













    • Good point, @Clemens - absolutely right! Updated my answer to clarify this to do with the shared brushes. Thanks!

      – Geoff James
      Dec 4 '17 at 22:32














    2












    2








    2







    As @Clemens kindly pointed out in the comments:



    You can't set the Opacity of the system's shared brushes directly.



    You will need to use a non-shared SolidColorBrush, and then you will be able to set the Opacity of that.



    You will be able to change the Opacity from any point in the code, from thereon-in.



    E.g.:



    moboBorder.Background = new SolidColorBrush(Color.FromRgb(255, 0, 0))
    {
    Opacity = 0.5 // or whatever opacity between
    // 0.0 (0%) and 1.0 (100%)
    };


    Or



    moboBorder.Background = new SolidColorBrush(Color.FromRgb(255, 0, 0));
    moboBorder.Background.Opacity = 0.5;




    Similar the above example, you could also set the alpha (the opacity) if you're using RGB.



    You can use the Color.FromArgb() static method, instead:



    moboBorder.Background = new SolidColorBrush(Color.FromArgb(0.5, 255, 0, 0));


    Just use a double between 0.0 and 1.0 (as before), as your first argument to the method.





    Hope this helps.






    share|improve this answer















    As @Clemens kindly pointed out in the comments:



    You can't set the Opacity of the system's shared brushes directly.



    You will need to use a non-shared SolidColorBrush, and then you will be able to set the Opacity of that.



    You will be able to change the Opacity from any point in the code, from thereon-in.



    E.g.:



    moboBorder.Background = new SolidColorBrush(Color.FromRgb(255, 0, 0))
    {
    Opacity = 0.5 // or whatever opacity between
    // 0.0 (0%) and 1.0 (100%)
    };


    Or



    moboBorder.Background = new SolidColorBrush(Color.FromRgb(255, 0, 0));
    moboBorder.Background.Opacity = 0.5;




    Similar the above example, you could also set the alpha (the opacity) if you're using RGB.



    You can use the Color.FromArgb() static method, instead:



    moboBorder.Background = new SolidColorBrush(Color.FromArgb(0.5, 255, 0, 0));


    Just use a double between 0.0 and 1.0 (as before), as your first argument to the method.





    Hope this helps.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 4 '17 at 23:04

























    answered Dec 4 '17 at 20:58









    Geoff JamesGeoff James

    2,3371725




    2,3371725








    • 1





      It's not true that "you can't set the Opacity of a Brush once it's in use". You just can't change the Opacity of the members of the Brushes class (as these are readonly). However, if you assign Background = new SolidColorBrush(Colors.Black) to an element, you can easily later set Background.Opacity = 0.5. Both answers should be edited.

      – Clemens
      Dec 4 '17 at 22:26













    • Good point, @Clemens - absolutely right! Updated my answer to clarify this to do with the shared brushes. Thanks!

      – Geoff James
      Dec 4 '17 at 22:32














    • 1





      It's not true that "you can't set the Opacity of a Brush once it's in use". You just can't change the Opacity of the members of the Brushes class (as these are readonly). However, if you assign Background = new SolidColorBrush(Colors.Black) to an element, you can easily later set Background.Opacity = 0.5. Both answers should be edited.

      – Clemens
      Dec 4 '17 at 22:26













    • Good point, @Clemens - absolutely right! Updated my answer to clarify this to do with the shared brushes. Thanks!

      – Geoff James
      Dec 4 '17 at 22:32








    1




    1





    It's not true that "you can't set the Opacity of a Brush once it's in use". You just can't change the Opacity of the members of the Brushes class (as these are readonly). However, if you assign Background = new SolidColorBrush(Colors.Black) to an element, you can easily later set Background.Opacity = 0.5. Both answers should be edited.

    – Clemens
    Dec 4 '17 at 22:26







    It's not true that "you can't set the Opacity of a Brush once it's in use". You just can't change the Opacity of the members of the Brushes class (as these are readonly). However, if you assign Background = new SolidColorBrush(Colors.Black) to an element, you can easily later set Background.Opacity = 0.5. Both answers should be edited.

    – Clemens
    Dec 4 '17 at 22:26















    Good point, @Clemens - absolutely right! Updated my answer to clarify this to do with the shared brushes. Thanks!

    – Geoff James
    Dec 4 '17 at 22:32





    Good point, @Clemens - absolutely right! Updated my answer to clarify this to do with the shared brushes. Thanks!

    – Geoff James
    Dec 4 '17 at 22:32











    2














    The equivalent of the XAML



    <Border.BorderBrush>
    <SolidColorBrush Color="Black" Opacity="0.7"/>
    </Border.BorderBrush>


    in code behind would be



    moboBorder.Background = new SolidColorBrush
    {
    Color = Colors.Black,
    Opacity = 0.7
    };


    In contrast to the predefined Brushes in the Brushes class (which are frozen), the above SolidColorBrush can be changed at any time later, like



    moboBorder.Background.Opacity = 0.5;





    share|improve this answer






























      2














      The equivalent of the XAML



      <Border.BorderBrush>
      <SolidColorBrush Color="Black" Opacity="0.7"/>
      </Border.BorderBrush>


      in code behind would be



      moboBorder.Background = new SolidColorBrush
      {
      Color = Colors.Black,
      Opacity = 0.7
      };


      In contrast to the predefined Brushes in the Brushes class (which are frozen), the above SolidColorBrush can be changed at any time later, like



      moboBorder.Background.Opacity = 0.5;





      share|improve this answer




























        2












        2








        2







        The equivalent of the XAML



        <Border.BorderBrush>
        <SolidColorBrush Color="Black" Opacity="0.7"/>
        </Border.BorderBrush>


        in code behind would be



        moboBorder.Background = new SolidColorBrush
        {
        Color = Colors.Black,
        Opacity = 0.7
        };


        In contrast to the predefined Brushes in the Brushes class (which are frozen), the above SolidColorBrush can be changed at any time later, like



        moboBorder.Background.Opacity = 0.5;





        share|improve this answer















        The equivalent of the XAML



        <Border.BorderBrush>
        <SolidColorBrush Color="Black" Opacity="0.7"/>
        </Border.BorderBrush>


        in code behind would be



        moboBorder.Background = new SolidColorBrush
        {
        Color = Colors.Black,
        Opacity = 0.7
        };


        In contrast to the predefined Brushes in the Brushes class (which are frozen), the above SolidColorBrush can be changed at any time later, like



        moboBorder.Background.Opacity = 0.5;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 5 '17 at 6:39

























        answered Dec 4 '17 at 22:30









        ClemensClemens

        90.2k894186




        90.2k894186






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47641658%2fsetting-wpf-background-opacity-programmatically%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            ts Property 'filter' does not exist on type '{}'

            Notepad++ export/extract a list of installed plugins