2011-05-30 24 views
1

我知道我們可以改變項目向右或向下使用ColumnSpan和行跨度Silverlight的電網 - 這並不符合它的細胞顯示值

不過,我有一個項目我想在網格單元格中顯示並已將其根據中心該小區中心,所以它會跨越權和文本中的情況下離開,我在這裏提供的是當前的外觀圖像,「12345」的文字無法適應列,並顯示爲「234」,是有一種方法,使1 & 5可見留下的所有列不變? enter image description here

文本可以更改爲任何內容並且必須正確居中,我可以通過這種方式破解12345的顯示方式,但需要一個可以處理任何文本的解決方案,以防以及textBlock grid.Column具有保持不變1(或2對我的情況精確),而不是0這個圖片的情況下。

回答

1

解決方案4
所以你實際上想要的是文本居中動態如果我現在理解正確的方式。你不希望自己擺弄填充,你想要一個適用於所有事情的數字嗎?我當然希望你感激這一點! :)

/// <summary> 
/// Takes a FrameworkElement that is spanned across 
/// multiple columns and RETURNS a Thickness that 
/// can be applied to the Padding property to 
/// centre it across the spanned columns 
/// </summary> 
/// <param name="fe">The FrameworkElement to be centred</param> 
/// <param name="centerColumn">The index of the desired centre column</param> 
/// <returns>A Thickness that will center the FrameworkElement</returns> 
private Thickness GetCenteringPadding(FrameworkElement fe, int centerColumn) 
{ 
    Grid parentGrid = fe.Parent as Grid; 
    if (parentGrid == null) 
     throw new ArgumentException(); 

    // Variables 
    int firstColumn = (int)fe.GetValue(Grid.ColumnProperty); 
    int columnSpan = (int)fe.GetValue(Grid.ColumnSpanProperty); 
    double totalWidth = 0.0; 
    double leftWidth = 0.0; 
    double leftPaddingWidth = 0.0; 

    // Total the width for all the spanned columns 
    for (int i = firstColumn; i < columnSpan + firstColumn; i++) 
    { 
     // This part can be dangerous, especially if you're using a '*' 
     totalWidth += parentGrid.ColumnDefinitions[i].ActualWidth; 
    } 

    // Get the total width from the left side of the first 
    // spanned column, to the center of the 'centerColumn' 
    for (int j = firstColumn; j <= centerColumn; j++) 
    { 
     if (j != centerColumn) 
      leftWidth += parentGrid.ColumnDefinitions[j].ActualWidth; 
     else // Only take half the width for the center column 
      leftWidth += parentGrid.ColumnDefinitions[j].ActualWidth/2; 
    } 

    // Calculate the padding width 
    // (Abbr. rightWidth = tW - lW, lPW = lW - rW) 
    leftPaddingWidth = 2*leftWidth - totalWidth; 

    // Check whether the padding needs to be on the left or the right 
    if (leftPaddingWidth > 0.0) 
    { 
     // The excess space is on the left 
     return new Thickness(leftPaddingWidth, 0.0, 0.0, 0.0); 
    } 
    else 
    { 
     // The excess space is on the right 
     return new Thickness(0.0, 0.0, -leftPaddingWidth, 0.0); 
    } 
} 

幾個筆記。它使用網格中列的.ActualWidth。當然,你可以將其更改爲.Width,但您可能當你使用ColumnDefinition Width="30*",因爲我敢肯定你不會得到雙重的結果,那麼遇到的問題。其次,此功能不會在您通過的對象上設置填充,原因有兩個:
- 您可以將返回的Thickness添加到已應用於Control.Padding,以便不破壞格式。
- 您可以通過任何FrameworkElement,而只有Controls有填充。也許你可以將返回的厚度添加到.Margin屬性中,或者甚至創建一個容器並在其中應用填充?
最後,您可以輕鬆地將兩個for loops合併到該函數中,但是我將它們分開以避免明確。
乾杯。

+0

你真的給它一些思考謝謝,病態標記爲答案,如果沒有更好的想法在幾天 – 2011-05-31 19:55:36

1

我不知道我完全理解你的問題的限制,所以它可能是這些方法都將工作,但這裏的兩個快速的建議。 (就個人而言,我喜歡解決方案2更好。)

<Edit: Additional Thought>
解決方案0
如果你能夠改變文本塊的Grid.ColumnSpan,你可以做的解決方案2,而該容器。只需將文本塊跨越幾列,並調整文本塊的填充以將內容居中!
</Edit>

解決方案1 ​​
首先,如果您當前的XAML結構看起來是這樣的:

<Grid> 
    <TextBlock Grid.Column="2" Text="12345" /> 
</Grid> 

考慮將其更改爲這樣的事情?

<Grid> 
    <!-- Draw up columns so that the textblock looks as you wish --> 
    <TextBlock Grid.Column="2" Text="12345" /> 

    <!-- Span an inner grid across all the columns/rows in the outer grid --> 
    <Grid Grid.ColumnSpan=".." 
      Grid.RowSpan=".."> 
     <!-- All the other stuff in the Grid --> 
    </Grid> 
</Grid> 

解決方案2
如果不能正常工作,考慮住房另一容器中的文本塊,如Label,並跨越跨列。

<Grid> 
    <!-- Use the left/right padding on the label to center the textbox --> 
    <sdk:Label Grid.Column="0" 
       Grid.ColumnSpan="3" 
       Grid.Row="1" 
       HorizontalAlignment="Center" 
       Padding="13,0,0,0"> 
     <!-- In my example, setting the left pading to 13 centered the textbox --> 
     <TextBlock HorizontalAlignment="Center" 
        Text="......!......" /> 
     <!-- Text like the test above, really helped find the correct padding --> 
    </sdk:Label> 
</Grid> 


我希望這個作品的限制之內,如果沒有,可以幫助你找出自己的解決方案!

+0

是啊解決方案2更像是一個黑客我說的是,解決方案一個不是很漂亮,你必須定義很多網格,想象我想有3列跨越左右,使用這種方法生病添加多個嵌套網格。解決方案0看起來是最好的3,它不需要編寫太多的代碼,但我希望有一種方法可能設置一些網格選項,以允許此類居中的內容在其單元格外部流動,而無需通過手動方式篡改通過中心TB填充..但這是相當不錯的解決方案,如果沒有更好的方法。 – 2011-05-30 17:09:41

+0

我真的不認爲有。最終,它歸結爲'Grid.Measure()'和'Grid.Arrange()'函數影響子對象佈局的方式。如果你重寫了'Grid'類(它需要從頭開始,因爲它是一個'sealed'類),或者重寫'TextBlock.Measure()'和'TextBlock.Arrange()'(再次, '密封')它可能工作,但我會說這是一個更不方便... :) – Melodatron 2011-05-31 08:37:47