没有标题空间的 WPF GroupBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2110148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF GroupBox with no header space
提问by PBelanger
Easy one, I would like to have a GroupBox with no header space
简单的一个,我想要一个没有标题空间的 GroupBox
alt text http://www.freeimagehosting.net/uploads/1d3f80b749.png
替代文字 http://www.freeimagehosting.net/uploads/1d3f80b749.png
The closest thing is a border, but the border "by default" does not have the same Style as the group box.
最接近的是边框,但边框“默认”与组合框的样式不同。
What's the easiest way (least xaml / code) to get the desired GroupBox ?
获得所需 GroupBox 的最简单方法(最少 xaml/代码)是什么?
Thanks
谢谢
采纳答案by mg007
If you really don't want a border, then there can be these 2 solutions:
如果您真的不想要边框,那么可以有以下两种解决方案:
(1) Edit template in blend :
(1) 在 blend 中编辑模板:
- Right click on GroupBox > Edit Template > Edit Copy > OK
Search for section
<Border.OpacityMask> <MultiBinding Converter="{StaticResource BorderGapMaskConverter}" ConverterParameter="7"> ...... </MultiBinding> </Border.OpacityMask>
Delete this (above mentioned) section.. You have just removed the "gap"
- Now this will work if you do not set the header (as you have shown in example). However if you set the header, it'll go behind the border. So to correct this, just set
Panel.ZIndex="-1"
in the border that was enclosing the section you just deleted (it looks like<Border BorderBrush="White" BorderThickness= ...
)
- 右键单击 GroupBox > 编辑模板 > 编辑副本 > 确定
搜索部分
<Border.OpacityMask> <MultiBinding Converter="{StaticResource BorderGapMaskConverter}" ConverterParameter="7"> ...... </MultiBinding> </Border.OpacityMask>
删除这个(上面提到的)部分..你刚刚删除了“差距”
- 现在,如果您不设置标题(如示例所示),这将起作用。但是,如果您设置标题,它将位于边框后面。因此,要纠正此问题,只需
Panel.ZIndex="-1"
在包围您刚刚删除的部分的边框中进行设置(看起来像<Border BorderBrush="White" BorderThickness= ...
)
(2) Use duplicate GroupBox and flip it horizontally and place it beneath original groupbox:
(2) 使用复制的 GroupBox 并将其水平翻转并将其放置在原始 groupbox 下方:
Put this code below your groupbox (assuming your groupbox's name is '
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>
Enclose both these GroupBox in a
Grid
like this:<Grid> <GroupBox x:Name="OriginalGroupbox" Header="Mihir" ...> ... </GroupBox> <GroupBox Header="" Width="{Binding ActualWidth, ElementName=OriginalGroupbox}" ...> ... </GroupBox> </Grid>
将此代码放在您的群组框下方(假设您的群组框名称为“
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>
Grid
像这样将这两个 GroupBox 括起来:<Grid> <GroupBox x:Name="OriginalGroupbox" Header="Mihir" ...> ... </GroupBox> <GroupBox Header="" Width="{Binding ActualWidth, ElementName=OriginalGroupbox}" ...> ... </GroupBox> </Grid>
回答by Ben Collier
You can emulate the style of the group box by changing your border to have rounded corners and a different color. Here is a border that looks pretty close to the GroupBox border:
您可以通过将边框更改为圆角和不同颜色来模拟组框的样式。这是一个看起来非常接近 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
回答by Locdarb
Building on Mihir Gokani's answer, you can change the default template to use a Trigger to change the header's padding to nothing, when the header is null or empty.
基于 Mihir Gokani 的回答,您可以更改默认模板以使用 Trigger 将标题的填充更改为空,当标题为 null 或为空时。
Use the following style on the GroupBox, should fix it.
在 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>
Note the only addition is:
注意唯一的补充是:
<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>
回答by Chris
the whole code and demo of its use
其使用的整个代码和演示
<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>
回答by LukeN
I was looking for a similar solution. I needed a group box style where the border was closed only when there is no header text.
我正在寻找类似的解决方案。我需要一个分组框样式,只有在没有标题文本时才关闭边框。
I'm not convinced it's the nicest solution, but it works fine...
我不相信这是最好的解决方案,但它工作正常......
We have a converter (works with text only atm):
我们有一个转换器(仅适用于文本 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
}
and the modifications to the groupbox style:
以及对 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>