WPF - 移动GroupBox标头

时间:2014-11-13 06:44:39

标签: c# wpf xaml

您好我有这个组合框模板。

<Style x:Key="bordesTransparentes" TargetType="{x:Type GroupBox}">
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="BorderThickness" Value="2"/>
        <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="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}" 
                                Grid.ColumnSpan="4" Grid.Row="1" Grid.Column="0" Grid.RowSpan="3">
                            <Border.OpacityMask>
                                <MultiBinding ConverterParameter="7" UpdateSourceTrigger="Default">
                                    <MultiBinding.Converter>
                                        <BorderGapMaskConverter/>
                                    </MultiBinding.Converter>
                                    <Binding Path="ActualWidth" ElementName="Header"/>
                                    <Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/>
                                    <Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}"/>
                                </MultiBinding>
                            </Border.OpacityMask>
                            <Border BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}">
                                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                            </Border>
                        </Border>
                        <Border x:Name="Header" Grid.Column="1" Padding="2,0,2,0" Grid.Row="0" Grid.RowSpan="2">
                            <ContentPresenter ContentTemplate="{TemplateBinding HeaderTemplate}" 
                                              Content="{TemplateBinding Header}" 
                                              ContentStringFormat="{TemplateBinding HeaderStringFormat}" 
                                              ContentSource="Header" 
                                              RecognizesAccessKey="True" 
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                        <ContentPresenter Grid.ColumnSpan="2" 
                                          ContentTemplate="{TemplateBinding ContentTemplate}" 
                                          Content="{TemplateBinding Content}" 
                                          Grid.Column="1" 
                                          ContentStringFormat="{TemplateBinding ContentStringFormat}" 
                                          Margin="{TemplateBinding Padding}" 
                                          Grid.Row="2" 
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我需要向右移动头部。尝试通过更改第一列和ConverterParameter(7到21)的宽度(6到20)来执行此操作,但包含的控件 在里面,也向右移动。一些解决方案

我需要这样的东西,但是没有文字背后的线条。

https://social.msdn.microsoft.com/Forums/getfile/564693

1 个答案:

答案 0 :(得分:1)

根据您提供的图片风格。

 <Window.Resources>
    <Style  TargetType="{x:Type GroupBox}">
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="BorderThickness" Value="2"/>
        <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" MinHeight="30"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="6"/>
                        </Grid.RowDefinitions>
                        <Grid  Grid.ColumnSpan="4" Grid.Row="0" Grid.RowSpan="2" Background="White">
                            <ContentPresenter TextElement.FontSize="22"  Grid.ColumnSpan="4" Grid.Row="0" Grid.RowSpan="2" ContentTemplate="{TemplateBinding HeaderTemplate}" 
                                          Content="{TemplateBinding Header}" 
                                          ContentStringFormat="{TemplateBinding HeaderStringFormat}" 
                                          ContentSource="Header"  HorizontalAlignment="Center" VerticalAlignment="Center"
                                          RecognizesAccessKey="True" 
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Grid>
                        <Border Margin="0,15,0,0" BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}" 
                            Grid.ColumnSpan="4"  Grid.Column="0" Grid.RowSpan="3">
                            <Border.OpacityMask>
                                <MultiBinding ConverterParameter="7" UpdateSourceTrigger="Default">
                                    <MultiBinding.Converter>
                                        <BorderGapMaskConverter/>
                                    </MultiBinding.Converter>
                                    <Binding Path="ActualWidth" ElementName="Header"/>
                                    <Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/>
                                    <Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}"/>
                                </MultiBinding>
                            </Border.OpacityMask>
                            <Border BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}">
                                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                            </Border>
                        </Border>

                        <ContentPresenter Grid.ColumnSpan="2" 
                                      ContentTemplate="{TemplateBinding ContentTemplate}" 
                                      Content="{TemplateBinding Content}" 
                                      Grid.Column="1" 
                                      ContentStringFormat="{TemplateBinding ContentStringFormat}" 
                                      Margin="{TemplateBinding Padding}" 
                                      Grid.Row="2" 
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<GroupBox Height="300" Width="600" Header="Datos a completor por el empleado" Margin="50">
    <StackPanel Margin="0,20,0,0" Height="30" VerticalAlignment="Top" Orientation="Horizontal">
        <TextBlock Text="Nombre de useario" FontSize="20"></TextBlock>
        <TextBox Height="30" Width="350" Margin="30,0,0,0"></TextBox>
    </StackPanel>
</GroupBox>

结果1 enter image description here


<Window.Resources>
    <Style  TargetType="{x:Type GroupBox}">
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="BorderThickness" Value="2"/>
        <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" MinHeight="30"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="6"/>
                        </Grid.RowDefinitions>
                        <Border Margin="0,15,0,0" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="4"  Grid.Column="0" Grid.RowSpan="3">
                            <Border.OpacityMask>
                                <MultiBinding ConverterParameter="7" UpdateSourceTrigger="Default">
                                    <MultiBinding.Converter>
                                        <BorderGapMaskConverter/>
                                    </MultiBinding.Converter>
                                    <Binding Path="ActualWidth" ElementName="Header"/>
                                    <Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/>
                                    <Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}"/>
                                </MultiBinding>
                            </Border.OpacityMask>
                            <Border BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}">
                                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                            </Border>
                        </Border>
                        <Grid  HorizontalAlignment="Right" Margin="0,0,20,0"  Grid.ColumnSpan="4" Grid.Row="0" Grid.RowSpan="2" Background="White">
                            <ContentPresenter TextElement.FontSize="22"  Grid.ColumnSpan="4" Grid.Row="0" Grid.RowSpan="2" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header"  HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Grid>
                        <ContentPresenter Grid.ColumnSpan="2" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1" ContentStringFormat="{TemplateBinding ContentStringFormat}" Margin="{TemplateBinding Padding}" Grid.Row="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<GroupBox Height="300" Width="600" Header="Datos a completor por el empleado" Margin="50">
    <StackPanel Margin="0,20,0,0" Height="30" VerticalAlignment="Top" Orientation="Horizontal">
        <TextBlock Text="Nombre de useario" FontSize="20"></TextBlock>
        <TextBox Height="30" Width="350" Margin="30,0,0,0"></TextBox>
    </StackPanel>
</GroupBox>

<强>结果2

enter image description here