WPF Groupbox 控件标题与按钮和文本对齐相反

Posted

技术标签:

【中文标题】WPF Groupbox 控件标题与按钮和文本对齐相反【英文标题】:WPF Groupbox Control Header with Button and text aligned Opposite 【发布时间】:2012-04-19 22:47:35 【问题描述】:

我想用WPF groupbox控件来实现这样的界面

有没有办法用WPF groupbox控件实现这样的界面?

【问题讨论】:

【参考方案1】:

一个简单的选择是只重叠控件,然后玩弄边距

    <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>

如果您想要一个可重复使用的样式,那么您需要提取 GroupBox 的控件模板并对其进行修改。类似的东西

<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>

【讨论】:

【参考方案2】:

我需要同样的东西,在这里找到了答案: http://wpf.2000things.com/2013/06/05/835-displaying-custom-content-in-a-groupbox-header/

不知道 .NET 的哪个版本

简单示例:

<GroupBox>
    <GroupBox.Header>
        <StackPanel Orientation="Horizontal">
            <Button Content="Click Me"/>
            <Button Content="Click Me Too"/>
        </StackPanel>
    </GroupBox.Header>
    <TextBlock Text="Content of box"/>
</GroupBox>

【讨论】:

以上是关于WPF Groupbox 控件标题与按钮和文本对齐相反的主要内容,如果未能解决你的问题,请参考以下文章

WPF - 组框标题对齐

WPF 控件

wpf控件

2021-08-17 WPF控件专题 Groupbox 控件详解

如何在单击按钮时复制和添加现有的网格元素(C#WPF)

WPF 基础控件之 GroupBox样式