77

I have a window with the following appearance:

enter image description here

What I would like, however, is if the Button controls (the gray ones with text in the middle) in the Window's main Grid had an opacity of 1, totally opaque. As I inherited this project the opacity was set to 0.75 at the top level, inside the opening Window tag. Now as I understand this will automatically enforce that on all children and that said children cannot override.

How then can I accomplish the transparent background but opaque buttons? The only way I have found so far (as a relative novice in WPF) is to have two separate Windows, one which is the transparent background, and the other has no background but contains the opaque controls. This is terribly hacky though and I want to avoid it if I can.

I can supply code if requested, but it is really as simple as a Window with windowstyle=none and opacity .75 containing a Grid, which contains some very basic Button etc controls.

Has anyone built such a Window before or otherwise have insight into generating one? Thanks.

kmarks2
  • 4,755
  • 10
  • 48
  • 77

3 Answers3

146

Instead of setting the opacity of the window, set its background's opacity:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        AllowsTransparency="True" WindowStyle="None">
    <Window.Background>
        <SolidColorBrush Opacity="0.5" Color="White"/>
    </Window.Background>
    <Grid>
        <Button Width="200" Height="50">button</Button>
    </Grid>
</Window>
svinja
  • 5,495
  • 5
  • 25
  • 43
  • 1
    Thank you so very much. I've tried so many things and I cannot believe it was this simple. – kmarks2 Jan 30 '14 at 16:20
  • @svinja, How can we achieve the same effect for WPF User Control which is being hosted in win forms. – RSB Feb 23 '15 at 14:03
  • @RSB set the user control's background brush to Transparent or Null – Gusdor Aug 05 '15 at 11:58
  • This is an important nuance, I think. I, too, was setting the control's properties of `Background` and `Opacity`. Using the property in this way provides much more control over how embedded controls look and feel. – DonBoitnott Jun 05 '18 at 15:39
3

If you create a style like this:

<Window.Resources>
    <Style TargetType="Button" x:Key="WindowButtons">
        <Setter Property="Opacity" Value="1"/>           
    </Style>
</Window.Resources>

Then you can reference those in the XAML for your button like this:

<Button Style="{StaticResource WindowButtons}">Tony</Button>

And it should no longer inherit it's opacity from its parent.

TylerD87
  • 1,588
  • 11
  • 20
1

Above effect can also be achieved by setting Opacity from designer from 100% to 60%(as required).

RSB
  • 359
  • 5
  • 10