WPF GroupBox没有标题空间

时间:2010-01-21 15:01:41

标签: wpf styles groupbox

简单的一个,我想有一个没有标题空间的GroupBox

alt text http://www.freeimagehosting.net/uploads/1d3f80b749.png

最接近的是边框,但边框“默认情况下”与组框的样式不同。

获得所需GroupBox的最简单方法(最少xaml /代码)是什么?

谢谢

5 个答案:

答案 0 :(得分:18)

如果你真的不想要边框,那么可以有以下两种解决方案:


(1)在混合中编辑模板:

  • 右键单击GroupBox>编辑模板>编辑复制>行
  • 搜索

    部分
      <Border.OpacityMask>
           <MultiBinding Converter="{StaticResource BorderGapMaskConverter}" ConverterParameter="7">
                ......
           </MultiBinding>
      </Border.OpacityMask>
    
  • 删除此(上述)部分..您刚刚删除了“差距”

  • 现在,如果您没有设置标题(如示例中所示),这将有效。但是,如果您设置标题,它将会在边框后面。因此,要更正此问题,只需在封闭刚刚删除的部分的边框中设置Panel.ZIndex="-1"(看起来像<Border BorderBrush="White" BorderThickness= ...

(2)使用重复的GroupBox并将其水平翻转并将其放在原始分组框下面:

  • 将此代码放在您的群组框下方(假设您的群组名称为“OriginalGroupbox”)

     <GroupBox Header="" Focusable="False" Panel.ZIndex="-1" 
               Width="{Binding ActualWidth, ElementName=OriginalGroupbox}" 
               Height="{Binding ActualHeight, ElementName=OriginalGroupbox}" 
               IsEnabled="{Binding IsEnabled, ElementName=OriginalGroupbox}"
               RenderTransformOrigin="0.5,0.5">
               <GroupBox.RenderTransform>
                    <ScaleTransform ScaleX="-1"/>
               </GroupBox.RenderTransform>
     </GroupBox>
    
  • 将这两个GroupBox包含在Grid中,如下所示:

    <Grid>
         <GroupBox x:Name="OriginalGroupbox" Header="Mihir" ...>
            ...
         </GroupBox>
         <GroupBox Header="" Width="{Binding ActualWidth, ElementName=OriginalGroupbox}" ...>
            ...
         </GroupBox>
    </Grid>
    

答案 1 :(得分:10)

您可以通过更改边框以使用圆角和不同颜色来模拟组框的样式。这是一个看起来非常接近GroupBox边框的边框:

<Border BorderThickness="1" CornerRadius="4" Height="100" Width="100" Padding="5" BorderBrush="LightGray"><TextBlock>Border</TextBlock></Border>

alt text http://img264.imageshack.us/img264/6748/borderm.png

答案 2 :(得分:5)

在Mihir Gokani的回答的基础上,当标题为空或为空时,您可以更改默认模板以使用触发器将标题的填充更改为空。

在GroupBox上使用以下样式,应该修复它。

<BorderGapMaskConverter x:Key="BorderGapMaskConverter"/>
<Style x:Key="GroupBoxStyle" 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>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="Header" Value="{x:Null}">
                        <Setter TargetName="Header" Property="Padding" Value="0" />                                 
                    </Trigger>
                        <Trigger Property="Header" Value="">
                            <Setter TargetName="Header" Property="Padding" Value="0" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

请注意,唯一的补充是:

<ControlTemplate.Triggers>
    <Trigger Property="Header" Value="{x:Null}">
        <Setter TargetName="Header" Property="Padding" Value="0" />                                 
    </Trigger>
    <Trigger Property="Header" Value="">
        <Setter TargetName="Header" Property="Padding" Value="0" />
    </Trigger>
</ControlTemplate.Triggers>

答案 3 :(得分:0)

我一直在寻找类似的解决方案。我需要一个组框样式,只有在没有标题文本时才关闭边框。

我不相信这是最好的解决方案,但它运作正常......

我们有一个转换器(仅使用文本atm):

public class GroupBoxHeaderVisibilityConverter : IMultiValueConverter
{
    #region IMultiValueConverter Members

    public object Convert(object[] values, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        ContentPresenter header = values[0] as ContentPresenter;
        if (header != null)
        {
            string text = header.Content as string;
            if (string.IsNullOrEmpty(text))
            {
                return 0.0;
            }
        }
        return values[1];
    }

    public object[] ConvertBack(object value, System.Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new System.NotImplementedException();
    }

    #endregion
}

以及对groupbox样式的修改:

<Border
    x:Name="Header"
    Grid.Column="1"
    Grid.Row="0"
    Grid.RowSpan="2"
    Padding="3,1,3,0">
    <Border.Tag>
        <MultiBinding Converter="{StaticResource GroupBoxHeaderVisibilityConverter}">
            <Binding Path="Content" ElementName="groupBoxLabel" />
            <Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}" />
        </MultiBinding> 
    </Border.Tag>
    <Label x:Name="groupBoxLabel"
        FontSize="{StaticResource Fonts_SmallFontSize}"
        Foreground="{TemplateBinding Foreground}">
        <ContentPresenter
            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
            ContentSource="Header"
            RecognizesAccessKey="True" />
    </Label>
</Border>
<ContentPresenter
    Margin="{TemplateBinding Padding}"
    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
    Grid.Column="1"
    Grid.ColumnSpan="2"
    Grid.Row="2" />
<Border
    Grid.ColumnSpan="4"
    Grid.Row="1"
    Grid.RowSpan="3"
    BorderBrush="Transparent"
    BorderThickness="{TemplateBinding BorderThickness}"
    CornerRadius="4">
    <Border.OpacityMask>                                
        <MultiBinding
            Converter="{StaticResource BorderGapMaskConverter}"
            ConverterParameter="7">
            <Binding ElementName="Header" Path="Tag" />
            <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>

答案 4 :(得分:0)

使用的整个代码和演示

<UserControl.Resources>
    <ResourceDictionary>
        <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 BorderBrush="White" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="4" CornerRadius="4" Grid.Row="1" Grid.RowSpan="3">

                            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3">
                                <Border BorderBrush="White" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2"/>
                            </Border>
                        </Border>
                        <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}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
        </ResourceDictionary>
</UserControl.Resources>

<Grid>

    <GroupBox Header="" HorizontalAlignment="Left" Margin="70,39,0,0" VerticalAlignment="Top" Height="169.96" Width="299.697" Style="{DynamicResource GroupBoxStyle1}"/>
</Grid>