wpf 设置 GroupBox 的样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9361903/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:18:39  来源:igfitidea点击:

Styling a GroupBox

wpfxamlstylinggroupbox

提问by gulbaek

I'm trying to create a GroupBox design like this.

我正在尝试创建这样的 GroupBox 设计。

enter image description here

在此处输入图片说明

I have looked at the GroupBox.HeaderTemplate

我看过了 GroupBox.HeaderTemplate

but I'm having problems creating the blue background color, with a width of 100%. The same goes for the border.

但我在创建宽度为 100% 的蓝色背景色时遇到问题。边界也是如此。

My code so far

到目前为止我的代码

<GroupBox.HeaderTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Label Content="{Binding}" HorizontalAlignment="Stretch" Background="#25A0DA" Grid.Column="0" Height="20" Padding="5,0,0,0" Margin="1" Foreground="White"/>
                    </Grid>
                </DataTemplate>
            </GroupBox.HeaderTemplate>

And this is what it looks like right now.

这就是它现在的样子。

enter image description here

在此处输入图片说明

回答by Rachel

Take the default GroupBox Templateand alter it to look the way you want

使用默认的GroupBox 模板并将其更改为您想要的样子

For example,

例如,

  <ControlTemplate TargetType="GroupBox">
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
      </Grid.RowDefinitions>

      <Border Grid.Row="0"
              BorderThickness="1"
              BorderBrush="#25A0DA"
              Background="#25A0DA">
          <Label Foreground="White">   
              <ContentPresenter Margin="4"
                          ContentSource="Header"
                          RecognizesAccessKey="True" />
          </Label>
      </Border>

      <Border Grid.Row="1"
              BorderThickness="1,0,1,1"
              BorderBrush="#25A0DA">
        <ContentPresenter Margin="4" />
      </Border>

    </Grid>
  </ControlTemplate>

回答by dave

This thread is a bit old, but someone could find this useful...

这个线程有点旧,但有人会发现这很有用......

A small modification to Jakob's answer if you want to have full width header.

如果您想要全宽标题,请对 Jakob 的答案稍作修改。

You can bind to the parent GroupBox, so you can use it without having a named GroupBox.

您可以绑定到父 GroupBox,因此您可以在没有命名 GroupBox 的情况下使用它。

<GroupBox.HeaderTemplate>
  <DataTemplate>
    <TextBlock Text="{Binding}" 
           Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=GroupBox}, Path=ActualWidth }"
           Background="#25A0DA" Grid.Column="0" Height="20" Padding="5,0,0,0" Margin="1" Foreground="White"/>
  </DataTemplate>
</GroupBox.HeaderTemplate>

回答by Jakob Christensen

You probably will not be able to make it look exactly like your example without writing a completely different template but I gave it a simple shot by binding the width of the grid in your HeaderTemplate to the width of the groupbox and by specifying appropriate margin and padding:

如果不编写完全不同的模板,您可能无法使它看起来与您的示例完全一样,但我通过将 HeaderTemplate 中的网格宽度绑定到 groupbox 的宽度并指定适当的边距和填充来给它一个简单的镜头:

<GroupBox.HeaderTemplate>
    <DataTemplate>
        <Grid Width="{Binding ElementName=groupBox1, Path=ActualWidth}" Margin="-10, 0, -10, 0" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Label Content="{Binding}" HorizontalAlignment="Stretch" Background="#25A0DA" Grid.Column="0" Height="20" Padding="5, 0, 0, 0" Margin="10" Foreground="White"/>
        </Grid>
    </DataTemplate>
</GroupBox.HeaderTemplate>

The result looks like this:

结果如下所示:

enter image description here

在此处输入图片说明

回答by yourbuddypal

I realize this is way late, but the MahApps.Metro package has a nice GroupBox that seems to like nearly exactly like what is posted in the OP.

我意识到这已经很晚了,但是 MahApps.Metro 包有一个不错的 GroupBox,它似乎与 OP 中发布的内容几乎一模一样。

https://github.com/MahApps/MahApps.Metro/blob/develop/MahApps.Metro/Styles/Controls.GroupBox.xaml

https://github.com/MahApps/MahApps.Metro/blob/develop/MahApps.Metro/Styles/Controls.GroupBox.xaml

Here's the Xaml from version 1.22

这是 1.22 版中的 Xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:Controls="clr-namespace:MahApps.Metro.Controls"
                xmlns:Converters="clr-namespace:MahApps.Metro.Converters">

<Converters:ThicknessBindingConverter x:Key="ThicknessBindingConverter" />

<Style x:Key="MetroGroupBox" TargetType="{x:Type GroupBox}">
    <Setter Property="Foreground" Value="{DynamicResource BlackBrush}" />
    <Setter Property="Background" Value="{DynamicResource AccentColorBrush}" />
    <Setter Property="BorderBrush" Value="{DynamicResource AccentColorBrush}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Controls:ControlsHelper.ContentCharacterCasing" Value="Upper" />
    <Setter Property="Controls:ControlsHelper.HeaderFontSize" Value="{DynamicResource ContentFontSize}" />
    <Setter Property="Controls:GroupBoxHelper.HeaderForeground" Value="{x:Null}" />
    <Setter Property="Margin" Value="5" />
    <Setter Property="Padding" Value="5" />
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupBox}">
                <Grid x:Name="GroupBoxRoot">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Border x:Name="HeaderSite"
                            Grid.Row="0"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                            UseLayoutRounding="True">
                        <Controls:ContentControlEx x:Name="HeaderContent"
                                                   Padding="{TemplateBinding Padding}"
                                                   FontSize="{TemplateBinding Controls:ControlsHelper.HeaderFontSize}"
                                                   FontWeight="{TemplateBinding Controls:ControlsHelper.HeaderFontWeight}"
                                                   FontStretch="{TemplateBinding Controls:ControlsHelper.HeaderFontStretch}"
                                                   Content="{TemplateBinding Header}"
                                                   ContentCharacterCasing="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ControlsHelper.ContentCharacterCasing)}"
                                                   ContentStringFormat="{TemplateBinding HeaderStringFormat}"
                                                   ContentTemplate="{TemplateBinding HeaderTemplate}"
                                                   ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
                                                   RecognizesAccessKey="True"
                                                   SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                   UseLayoutRounding="False">
                            <TextElement.Foreground>
                                <MultiBinding Converter="{x:Static Converters:BackgroundToForegroundConverter.Instance}">
                                    <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                             Path="Background"
                                             Mode="OneWay" />
                                    <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                             Path="(Controls:GroupBoxHelper.HeaderForeground)"
                                             Mode="OneWay" />
                                </MultiBinding>
                            </TextElement.Foreground>
                        </Controls:ContentControlEx>
                    </Border>
                    <Border Grid.Row="1"
                            Background="Transparent"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness, Converter={StaticResource ThicknessBindingConverter}, ConverterParameter={x:Static Converters:IgnoreThicknessSideType.Top}}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                            UseLayoutRounding="True">
                        <ContentPresenter Margin="{TemplateBinding Padding}"
                                          Content="{TemplateBinding Content}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}"
                                          Cursor="{TemplateBinding Cursor}"
                                          UseLayoutRounding="False" />
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

回答by Hotman Jawa

Try this :

尝试这个 :

  <GroupBox BorderThickness="0"  Header="BELT WEIGHER BC#1 JETTY" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="193" Width="374" OpacityMask="Black" BorderBrush="#FF1864D3" FontSize="16" FontWeight="Bold">
        <GroupBox.HeaderTemplate >
            <DataTemplate>
                <TextBlock Text="{Binding}"  
                 Width="357" Grid.Column="0" Padding="5,3,0,0" Margin="0,0,0,0"  Foreground="White" Height="33">
                    <TextBlock.Background>
                        <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
                            <GradientStop Color="White" Offset="0.968"/>
                            <GradientStop Color="Blue" Offset="0.828"/>
                        </LinearGradientBrush>
                    </TextBlock.Background>
                </TextBlock>
            </DataTemplate>
        </GroupBox.HeaderTemplate>
        <Border x:Name="CanvasBorder" BorderBrush="Blue"  BorderThickness="1" Margin="3,0,3,0">
            <Canvas  Background="white" Margin="0,0,0,0" >
                <Label Content="Feed Rate" Canvas.Left="10" Canvas.Top="10" FontWeight="Normal" FontSize="12"/>
                <Label Content="Totalizer 1" Canvas.Left="10" Canvas.Top="35" FontWeight="Normal" FontSize="12"/>
                <Label Content="Totalizer 2" Canvas.Left="10" Canvas.Top="60" FontWeight="Normal" FontSize="12" RenderTransformOrigin="0.516,1.808"/>
                <Label Content="Belt Load" Canvas.Left="10" Canvas.Top="85" FontWeight="Normal" FontSize="12" RenderTransformOrigin="0.516,1.808"/>
                <Label Content="Belt Speed" Canvas.Left="10" Canvas.Top="110" FontWeight="Normal" FontSize="12" RenderTransformOrigin="0.516,1.808"/>
                <TextBlock x:Name="BC1_Feedrate" HorizontalAlignment="Left" TextWrapping="Wrap" Text="0.00" VerticalAlignment="Top" Background="#FFF2F2FB" Width="119" FontWeight="Bold" Height="20" TextAlignment="Right" Canvas.Left="91" Canvas.Top="13" Padding="0,0,4,0"/>
                <TextBlock x:Name="BC1_Totalizer1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="0.00" VerticalAlignment="Top" Background="#FFF2F2FB" Width="119" FontWeight="Bold" Height="20" TextAlignment="Right" Canvas.Left="91" Canvas.Top="38" Padding="0,0,4,0"/>
                <TextBlock x:Name="BC1_Totalizer2" HorizontalAlignment="Left" TextWrapping="Wrap" Text="0.00" VerticalAlignment="Top" Background="#FFF2F2FB" Width="119" FontWeight="Bold" Height="20" TextAlignment="Right" Canvas.Left="91" Canvas.Top="63" Padding="0,0,4,0"/>
                <TextBlock x:Name="BC1_BeltLoad" HorizontalAlignment="Left" TextWrapping="Wrap" Text="0.00" VerticalAlignment="Top" Background="#FFF2F2FB" Width="119" FontWeight="Bold" Height="20" TextAlignment="Right" Canvas.Left="91" Canvas.Top="88" Padding="0,0,4,0"/>
                <TextBlock x:Name="BC1_BeltSpeed" HorizontalAlignment="Left" TextWrapping="Wrap" Text="0.00" VerticalAlignment="Top" Background="#FFF2F2FB" Width="119" FontWeight="Bold" Height="20" TextAlignment="Right" Canvas.Left="91" Canvas.Top="113" Padding="0,0,4,0"/>
                <Label Content="ton/hour" Canvas.Left="228" Canvas.Top="10" FontWeight="Normal" FontSize="12"/>
                <Label Content="ton" Canvas.Left="228" Canvas.Top="35" FontWeight="Normal" FontSize="12"/>
                <Label Content="ton" Canvas.Left="228" Canvas.Top="60" FontWeight="Normal" FontSize="12" RenderTransformOrigin="0.516,1.808"/>
                <Label Content="kg/meter" Canvas.Left="228" Canvas.Top="85" FontWeight="Normal" FontSize="12" RenderTransformOrigin="0.516,1.808"/>
                <Label Content="meter/second" Canvas.Left="228" Canvas.Top="110" FontWeight="Normal" FontSize="12" RenderTransformOrigin="0.516,1.808"/>
            </Canvas>
        </Border>
    </GroupBox>