2012-10-16 68 views
0

所以我有一個網格列中的窗口按鈕。按鈕如下所示:WPF按鈕,內容到達兩側

<Button> 
    <Border BorderThickness="3" BorderBrush="Red"> 
     <Grid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button, AncestorLevel=1}, Path=Width}"> 
      <Grid.RowDefinitions> 
       <RowDefinition/> 
       <RowDefinition/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 
       <TextBlock Grid.Row="0" Background="LightBlue">bunnies</TextBlock> 
      <TextBlock Grid.Row="1" Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button, AncestorLevel=1}, Path=Width}" Background="LightBlue">bunnies</TextBlock> 
     </Grid> 
    </Border> 
</Button> 

我看到的是下面最左邊的按鈕中的示例,我希望它看起來像下面的2。

編輯: 如果您向第一個TextBlock添加Width =「1000」,您將看到我想要的效果,除了文本不再是該列的中心位置,它是1000寬度塊的核心。所以我所追求的是實際列寬或實際按鈕寬度的綁定語法。

我已經嘗試了一些我已經嘗試過的事情,改變了單個列定義的寬度,綁定的東西,各種各樣。

Left = 1 Right = 2

忽略的文本編輯狡猾,我只想邊境到達按鈕的邊緣。 感謝

+0

所以,你要舒展文本,如果它不夠長? –

+0

對不起,這不是我的不良MSPaint技能。忽略文本,如果文本很短我仍然希望邊框適合列的寬度。你可以通過將TextBlock的Width設置爲一個實際的數字來僞造它,但是我找不到如何綁定到按鈕的實際寬度。 – Ian

+0

添加了一個編輯,希望這有點更清楚:) – Ian

回答

2

試試這個:

<Button HorizontalContentAlignment="Stretch"> 
      <Border BorderThickness="3" BorderBrush="Red"> 
       <Grid HorizontalAlignment="Stretch"> 
        <Grid.RowDefinitions> 
         <RowDefinition/> 
         <RowDefinition/> 
        </Grid.RowDefinitions> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="*"/> 
        </Grid.ColumnDefinitions> 
        <TextBlock Grid.Row="0" TextAlignment="Center" Background="LightBlue">bunnies</TextBlock> 
        <TextBlock Grid.Row="1" TextAlignment="Center" Background="LightBlue">bunnies</TextBlock> 
       </Grid> 
      </Border> 
     </Button> 
+0

完美。我喜歡它,當答案直截了當時:)我不知道爲什麼,但我發現WPF中的intellisense有點奇怪。剛剛檢查,這是怎麼回事。如果我命名該按鈕,並在代碼中開始鍵入「AButton.Alig」,它會給我四種不同的路線作爲選項。它在WPF編輯器中沒有這樣做。可能會把這些回饋給resharper人,除非它只是我做錯了:) – Ian