Search code examples
wpfopacity

WPF, how to adjust groupbox transparency


I have a WPF application with a nice background picture. However, when I place some elements to the form I want them to be not so transparent.

For example simple groupbox:

   <GroupBox x:Name="LocationGroup" Grid.Column="1" Grid.Row="1" Header="Location" HorizontalAlignment="Left" Height="100" Margin="10,90,0,0" VerticalAlignment="Top" Width="734">
        <Grid Margin="1,1,1,1">

            <Label Content="Location" HorizontalAlignment="Left" Margin="5,5,0,0" VerticalAlignment="Top"/>
            <ComboBox x:Name="LocationCombo" HorizontalAlignment="Left" Margin="5,36,0,0" VerticalAlignment="Top" Width="100"
                      DisplayMemberPath="LocationDescr" SelectedValuePath="LocationNr" SelectedValue="{Binding Path=Location}">
            </ComboBox>

        </Grid>
    </GroupBox>

I have tried setting the group box (and grid) opacity properties, but it only affects to the label and combo box opacity, not the background.

What I'm looking for is just like the element in the right in the picture linked below:

Opacity example


Solution

  • Here's one way you can achieve it:

     <Grid>
        <Grid.Background>
            <ImageBrush ImageSource="path\to\backgroundimage"  Stretch="UniformToFill"/>
        </Grid.Background>
    
        <Grid Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" Height="100" Width="400" Margin="10,90,0,0" VerticalAlignment="Top">
            <Grid Background="White" Opacity="0.5"></Grid>
            <GroupBox x:Name="LocationGroup" Header="Location">
                <Grid Margin="1,1,1,1">
                    <Label Content="Location" HorizontalAlignment="Left" Margin="5,5,0,0" VerticalAlignment="Top"/>
                    <ComboBox x:Name="LocationCombo" HorizontalAlignment="Left" Margin="5,36,0,0" VerticalAlignment="Top" Width="100"
                      DisplayMemberPath="LocationDescr" SelectedValuePath="LocationNr" SelectedValue="{Binding Path=Location}">
                    </ComboBox>
                </Grid>
            </GroupBox>
        </Grid>
    </Grid>