Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Groupbox Control Header with Button and text aligned Opposite

I want to achieve such an interface using WPF groupbox control

enter image description here

Is there a way to achieve such an interface with WPF groupbox control?

like image 571
Asad Durrani Avatar asked Apr 06 '12 09:04

Asad Durrani


2 Answers

A simple option is to just overlap the controls and then play around with margins

    <Grid>
        <GroupBox Header="Title" Margin="0,3,0,0" />
        <StackPanel Orientation="Horizontal" 
            VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,0,10,0">
            <Button Margin="2" Content="Suchen"/>
            <Button Margin="2" Content="Neu"/>
        </StackPanel>
    </Grid>

If you wanted a re-usable style then you would need to extract the GroupBox's control template and modify that. Something like

<Page.Resources>
    <BorderGapMaskConverter x:Key="BorderGapMaskConverter"/>
    <Style x:Key="GroupBoxStyle1" TargetType="{x:Type GroupBox}">
        <Setter Property="BorderBrush" Value="#D5DFE5"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupBox}">
                    <Grid SnapsToDevicePixels="true">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="6"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="6"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="6"/>
                        </Grid.RowDefinitions>
                        <Border BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="4" Grid.Column="0" CornerRadius="4" Grid.Row="1" Grid.RowSpan="3"/>
                        <Border x:Name="Header" Grid.Column="1" Padding="3,1,3,0" Grid.Row="0" Grid.RowSpan="2">
                            <ContentPresenter ContentSource="Header" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                        <ContentPresenter Grid.ColumnSpan="2" Grid.Column="1" Margin="{TemplateBinding Padding}" Grid.Row="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        <Border BorderBrush="White" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="4" CornerRadius="4" Grid.Row="1" Grid.RowSpan="3">
                            <Border.OpacityMask>
                                <MultiBinding ConverterParameter="7" Converter="{StaticResource BorderGapMaskConverter}">
                                    <Binding ElementName="Header" Path="ActualWidth"/>
                                    <Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/>
                                    <Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}"/>
                                </MultiBinding>
                            </Border.OpacityMask>
                            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3">
                                <Border BorderBrush="White" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2"/>
                            </Border>
                        </Border>
                        <StackPanel Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="0" Grid.RowSpan="3" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right" >
                            <Button Margin="2" Content="Suchen"/>
                            <Button Margin="2" Content="Neu"/>
                        </StackPanel>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>

<Grid>
    <Grid>
        <GroupBox Header="Title" Style="{StaticResource GroupBoxStyle1}"></GroupBox>
    </Grid>
</Grid>
like image 163
Phil Avatar answered Oct 19 '22 22:10

Phil


I needed the same thing, found the answer here: http://wpf.2000things.com/2013/06/05/835-displaying-custom-content-in-a-groupbox-header/

Don't know which version of .NET

Simple example:

<GroupBox>
    <GroupBox.Header>
        <StackPanel Orientation="Horizontal">
            <Button Content="Click Me"/>
            <Button Content="Click Me Too"/>
        </StackPanel>
    </GroupBox.Header>
    <TextBlock Text="Content of box"/>
</GroupBox>
like image 33
luytsm Avatar answered Oct 19 '22 22:10

luytsm