2014-02-28 87 views
0

我新的WPF,WPF - 無法對齊GROUPBOX頭向右

我不能對齊組框標題文本「ABC」的權利,這是在左邊的住宿,不知道爲什麼,任何人都可以幫助我?

<Window x:Class="UserInterface.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="500" Width="625"> 
    <Grid ShowGridLines="True"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/>    
      <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 
     <Grid Grid.Column="0" ShowGridLines="True"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="4*"/> 
       <RowDefinition Height="*"/> 
      </Grid.RowDefinitions> 
      <StackPanel Grid.Row="1"> 
       <GroupBox> 
        <GroupBox.Header> 
         <DockPanel> 
          <TextBlock HorizontalAlignment="Right">abc</TextBlock> 
         </DockPanel> 
        </GroupBox.Header> 
       </GroupBox> 
      </StackPanel> 
     </Grid> 
    </Grid> 
</Window> 
+0

你必須編輯'GroupBox'的'ControlTemplate'作爲頭硬編碼留在那裏使用對齊'網格'和欄目安排。 –

+0

http://stackoverflow.com/questions/2104013/wpf-groupbox-header-postion-alignment – har07

回答

1

你必須在案件的Groupbox覆蓋Template要對齊頭Right。默認情況下,它默認放置在默認模板中。

關鍵是

  1. 套裝Grid.ColumnSpan2邊境託管頭ContentPresenter。在ContentPresenter上設置HorizontalAlignmentRight

這裏是一個將工作的XAML:

<GroupBox Header="abc"> 
    <GroupBox.Template> 
     <ControlTemplate TargetType="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 BorderThickness="{TemplateBinding Border.BorderThickness}" 
        CornerRadius="4,4,4,4" 
        BorderBrush="#00FFFFFF" 
        Background="{TemplateBinding Panel.Background}" 
        Grid.Column="0" 
        Grid.Row="1" 
        Grid.ColumnSpan="4" 
        Grid.RowSpan="3" /> 
      <Border BorderThickness="{TemplateBinding Border.BorderThickness}" 
        CornerRadius="4,4,4,4" 
        BorderBrush="#FFFFFFFF" 
        OpacityMask="{x:Null}" 
        Grid.Row="1" 
        Grid.ColumnSpan="4" 
        Grid.RowSpan="3"> 
       <Border BorderThickness="{TemplateBinding Border.BorderThickness}" 
         CornerRadius="3,3,3,3" 
         BorderBrush="{TemplateBinding Border.BorderBrush}"> 
       <Border BorderThickness="{TemplateBinding Border.BorderThickness}" 
         CornerRadius="2,2,2,2" 
         BorderBrush="#FFFFFFFF" /> 
       </Border> 
      </Border> 
      <Border Padding="3,1,3,0" 
        Name="Header" 
        Grid.Column="1" 
        Grid.ColumnSpan="2" <-- HERE 
        Grid.Row="0" 
        Grid.RowSpan="2"> 
       <ContentPresenter RecognizesAccessKey="True" 
           HorizontalAlignment="Right" <-- And HERE 
           Content="{TemplateBinding HeaderedContentControl.Header}" 
           ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}" 
           ContentStringFormat="{TemplateBinding HeaderedContentControl.HeaderStringFormat}" 
           ContentSource="Header" 
           SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /> 
      </Border> 
      <ContentPresenter Content="{TemplateBinding ContentControl.Content}" 
           ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" 
           ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" 
           Margin="{TemplateBinding Control.Padding}" 
           SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" 
           Grid.Column="1" 
           Grid.Row="2" 
           Grid.ColumnSpan="2" /> 
      </Grid> 
     </ControlTemplate> 
    </GroupBox.Template> 
</GroupBox> 
0

您需要重新設計控制的模板內的ContentPresenter。

MSDN有一個例子,但不知何故它也重新設計了GroupBox。

檢查了這一點,看看你是否在找什麼。您正在尋找此行

<ContentPresenter Margin="4" 
        ContentSource="Header" 
        RecognizesAccessKey="True" /> 

只需添加到:

HorizontalAlignment="Right"