更改Groupbox标头方向

时间:2012-09-25 10:34:34

标签: wpf xaml groupbox

我在WPF中创建一个包含控件的组框,并想知道是否可以将标题更改为垂直而不是水平运行?这是我的代码;

<GroupBox Grid.Column="0">
    <GroupBox.Header>
        Navigation
    </GroupBox.Header>
    <StackPanel Orientation="Vertical" >
        <Button>Up</Button>
        <Button>Down</Button>
    </StackPanel>
</GroupBox>

我已经探索了这些属性,但除了改变GroupBox.Header元素中包含的TextBlock之外,似乎找不到任何其他内容。

<GroupBox Grid.Column="0">
    <GroupBox.Header>
        <Orientation>
           <!-- Invalid -->
        </Orientation>
    </GroupBox.Header>
    <StackPanel Orientation="Vertical" >
        <Button>Up</Button>
        <Button>Down</Button>
    </StackPanel>
</GroupBox>

3 个答案:

答案 0 :(得分:1)

我认为只要更改属性或使用标题模板,就不能让组框标题垂直运行。相反,您可以尝试RotateTransform,但它也会旋转组框内的所有元素。因此,您必须分别为组框内的容器元素提供计数器转换。

<GroupBox Header="Navigation" Grid.Column="0">
    <GroupBox.RenderTransform>
        <RotateTransform Angle="-90" CenterX="100" CenterY="100" />
    </GroupBox.RenderTransform>
    <StackPanel Orientation="Vertical">
        <StackPanel.RenderTransform>
            <RotateTransform Angle="90" CenterX="100" CenterY="100"  />
        </StackPanel.RenderTransform>
        <Button>Up</Button>
        <Button>Down</Button>
    </StackPanel>
</GroupBox>

让元素对齐会很混乱,你必须整天玩CenterXCenterY属性。但如果这就是你想要的,我猜这是获得它的方法:)

答案 1 :(得分:1)

我可以想象组框中有两个垂直标题选项:

1)旋转文本块的“流”但不旋转字母

2)旋转文本块本身

以下是XAML中的这两个选项:

    <Grid>
        <Grid.Resources>
            <System:String x:Key="header">Vertical</System:String>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <GroupBox Grid.Row="0">
            <GroupBox.HeaderTemplate>
                <DataTemplate>
                    <ItemsControl ItemsSource="{StaticResource header}" />
                </DataTemplate>
            </GroupBox.HeaderTemplate>
        </GroupBox>
        <GroupBox Grid.Row="1">
            <GroupBox.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{StaticResource header}">
                        <TextBlock.LayoutTransform>
                            <RotateTransform Angle="90"  />
                        </TextBlock.LayoutTransform>
                    </TextBlock>
                </DataTemplate>
            </GroupBox.HeaderTemplate>
        </GroupBox>
    </Grid>

答案 2 :(得分:1)

你可以,但它不漂亮! Badly rotated header

<GroupBox>
    <GroupBox.Header>
        <TextBlock Text="Hello">
            <TextBlock.RenderTransform>
                 <RotateTransform Angle="90" CenterX="0" CenterY="0"  /> 
            </TextBlock.RenderTransform>
        </TextBlock>
    </GroupBox.Header>
  <TextBlock Text="World!"/>
</GroupBox>

您还需要修改style of the GroupBox以支持此功能。

这看起来只是一个有几行的网格,几个内容演示者和几个边框,因此应该可以将其转换为列并从那里开始。我实际上将Rotate放在执行标题的ContentPresenter上,这样你就可以在任何你想要的地方应用你的风格。

最终更新

通过从标准控件中提取模板,我们可以对其进行修改以将标题移动,但是我们还发现有一个转换器用于屏蔽周围的边框。通过使用反编译器(DotPeek),我们还可以围绕行和列切换以将间隙移到侧面。

Rotated GroupBox

所以模板看起来像这样

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="MainWindow" Height="350" Width="525">
<Window.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="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="6"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="6"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="6"/>
                        </Grid.RowDefinitions>
                        <Border BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="3" Grid.Column="1" CornerRadius="4" Grid.Row="0" Grid.RowSpan="4"/>
                        <Border x:Name="Header" Grid.Column="0" Padding="3,1,3,0" Grid.Row="1" Grid.ColumnSpan="2">
                            <Border.LayoutTransform>
                                <RotateTransform Angle="-90"/>
                            </Border.LayoutTransform>
                            <ContentPresenter ContentSource="Header" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                        <ContentPresenter Grid.RowSpan="2" Grid.Row="1" Margin="{TemplateBinding Padding}" Grid.Column="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        <Border BorderBrush="White" BorderThickness="{TemplateBinding BorderThickness}" Grid.RowSpan="4" CornerRadius="4" Grid.Column="1" Grid.ColumnSpan="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>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>


<GroupBox Header="Hello world!" Margin="12" Style="{DynamicResource GroupBoxStyle1}"/>
</Window>

修改后的转换器看起来像这样

// Type: System.Windows.Controls.BorderGapMaskConverter
// Assembly: PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Assembly location: C:\Windows\Microsoft.NET\assembly\GAC_MSIL\PresentationFramework\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.dll

using System;
using System.Globalization;
using System.Runtime;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WfpApplication1 //System.Windows.Controls
{
    public class LeftBorderGapMaskConverter : IMultiValueConverter
{
    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    public LeftBorderGapMaskConverter()
    {
        //      base.ctor();
    }

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        Type type1 = typeof(double);
        if (parameter == null
            || values == null
            || (values.Length != 3 || values[0] == null)
            || (values[1] == null
                || values[2] == null
                || (!type1.IsAssignableFrom(values[0].GetType())
                    || !type1.IsAssignableFrom(values[1].GetType())))
            || !type1.IsAssignableFrom(values[2].GetType()))
            return DependencyProperty.UnsetValue;

        Type type2 = parameter.GetType();
        if (!type1.IsAssignableFrom(type2)
            && !typeof(string).IsAssignableFrom(type2))
            return DependencyProperty.UnsetValue;

        double pixels1 = (double)values[0];
        double num1 = (double)values[1];
        double num2 = (double)values[2];
        if (num1 == 0.0 || num2 == 0.0)
            return (object)null;

        double pixels2 = !(parameter is string)
            ? (double)parameter
            : double.Parse((string)parameter, (IFormatProvider)NumberFormatInfo.InvariantInfo);

        Grid grid = new Grid();
        grid.Width = num1;
        grid.Height = num2;
        RowDefinition RowDefinition1 = new RowDefinition();
        RowDefinition RowDefinition2 = new RowDefinition();
        RowDefinition RowDefinition3 = new RowDefinition();
        RowDefinition1.Height = new GridLength(pixels2);
        RowDefinition2.Height = new GridLength(pixels1);
        RowDefinition3.Height = new GridLength(1.0, GridUnitType.Star);
        grid.RowDefinitions.Add(RowDefinition1);
        grid.RowDefinitions.Add(RowDefinition2);
        grid.RowDefinitions.Add(RowDefinition3);
        ColumnDefinition ColumnDefinition1 = new ColumnDefinition();
        ColumnDefinition ColumnDefinition2 = new ColumnDefinition();
        ColumnDefinition1.Width = new GridLength(num2 / 2.0);
        ColumnDefinition2.Width = new GridLength(1.0, GridUnitType.Star);
        grid.ColumnDefinitions.Add(ColumnDefinition1);
        grid.ColumnDefinitions.Add(ColumnDefinition2);
        Rectangle rectangle1 = new Rectangle();
        Rectangle rectangle2 = new Rectangle();
        Rectangle rectangle3 = new Rectangle();
        rectangle1.Fill = (Brush)Brushes.Black;
        rectangle2.Fill = (Brush)Brushes.Black;
        rectangle3.Fill = (Brush)Brushes.Black;

        Grid.SetColumnSpan((UIElement)rectangle1, 2);
        Grid.SetColumn((UIElement)rectangle1, 0);
        Grid.SetRow((UIElement)rectangle1, 0);
        Grid.SetColumn((UIElement)rectangle2, 1);
        Grid.SetRow((UIElement)rectangle2, 1);
        Grid.SetColumnSpan((UIElement)rectangle3, 2);
        Grid.SetColumn((UIElement)rectangle3, 0);
        Grid.SetRow((UIElement)rectangle3, 2);
        grid.Children.Add((UIElement)rectangle1);
        grid.Children.Add((UIElement)rectangle2);
        grid.Children.Add((UIElement)rectangle3);
        return (object)new VisualBrush((Visual)grid);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return new object[1]
          {
            Binding.DoNothing
          };
    }
}
}