2016-02-11 90 views
1

我正在製作WPF中的多選項卡應用程序,並且我想在選項卡的網格中製作一個水平居中的文本框。該選項卡控件當前被拉伸以適應窗口大小,因此如果窗口被調整大小/最大化,應用程序也將更改大小。如果我按下設計器中的「水平居中」按鈕,則會將文本框的方式置於右側。我在這裏做錯了什麼?拉伸選項卡控件中的中心文本框

下面是應用程序的XAML代碼至今:

<Window x:Class="GUI_Test.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:GUI_Test" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="524" Width="996.432"> 
    <Grid> 
     <TabControl x:Name="tabControl" HorizontalContentAlignment="Center"> 
      <TabItem x:Name="Home" Header="Home" Width="150"> 
       <Grid Background="#FFE5E5E5"/> 
      </TabItem> 
      <TabItem x:Name="Dictionary" Header="Dictionary" Width="150"> 
       <Grid Background="#FFE5E5E5"/> 
      </TabItem> 
      <TabItem x:Name="Search" Header="Search" Height="20" Width="150"> 
       <Grid Background="#FFE5E5E5"> 
        <TextBox x:Name="textBox" HorizontalAlignment="Center" Height="23" Margin="524,105,204,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="250"/> 
       </Grid> 
      </TabItem> 
      <TabItem x:Name="About" Header="About" Height="20" Width="150"> 
       <Grid Background="#FFE5E5E5"/> 
      </TabItem> 
     </TabControl> 
    </Grid> 
</Window> 

我關注的「搜索」選項卡上,在這一刻。當窗口展開時,文本框(以及其他任何元素)也需要移動,所以我不能只計算中點並設置尺寸。我會做類似MarginLeft = MainWindow.getWidth/2或類似的東西,但是既沒有MarginLeft也沒有MainWindow有任何可編輯的屬性。 我也嘗試在選項卡中使網格佈局不同,但它不允許我使用。

+2

請給我們展示一些代碼。請參閱http://stackoverflow.com/help/mcve –

+0

謝謝。它已被編輯。對不起,我不太流利的StackOverflow的規則呢:) – mmf1102

+0

從文本框中刪除邊距。 –

回答

0

如果我有你的權利,你想水平集中在GridTextBox,你必須要設置HorizontalAlignmentCenter,並卸下左,右側兩個Margin(第一和第三個數字),這樣:

<TextBox x:Name="textBox" HorizontalAlignment="Center" Height="23" Margin="0,105,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="250"/> 

強調Margin="0,105,0,0",這是你的代碼有什麼不同。

如果你想它集中垂直過,剛剛成立VerticalAlignmentCenter,並徹底清除Margin(或將其設置爲零)。

+0

工作,謝謝。我看到問題出現的地方,與利益衝突。 – mmf1102

相關問題