2012-07-23 78 views
12

VariableSizedWrapGridWrapGrid都有奇怪的測量 - 他們根據第一項測量所有孩子。VariableSizedWrapGrid和WrapGrid兒童尺寸的測量

因此,以下XAML將剪切第三項。

<VariableSizedWrapGrid Orientation="Horizontal"> 
     <Rectangle Width="50" Height="100" Margin="5" Fill="Blue" /> 
     <Rectangle Width="50" Height="50" Margin="5" Fill="Red" /> 
     <Rectangle Width="50" Height="150" Margin="5" Fill="Green" /> 
     <Rectangle Width="50" Height="50" Margin="5" Fill="Red" /> 
     <Rectangle Width="50" Height="100" Margin="5" Fill="Red" /> 
    </VariableSizedWrapGrid> 

好像VariableSizedWrapGrid措施中的第一項,然後將其餘的兒童與第一個的期望的尺寸進行測定。

任何解決方法?

回答

3

您需要使用每個矩形VariableSizeWrapGrid.ColumnSpan和VariableSizeWrapGrid.RowSpan的附加屬性以及添加ItemHeight和ItemWidth到VariableSizeWrapGrid:

<VariableSizedWrapGrid Orientation="Horizontal" ItemHeight="50" ItemWidth="50"> 
    <Rectangle 
     VariableSizedWrapGrid.ColumnSpan="1" 
     VariableSizedWrapGrid.RowSpan="2" 
     Width="50" Height="100" Margin="5" Fill="Blue" /> 
</VariableSizedWrapGrid> 
0

它可能不是最好的方式,但是這是如何我做這在我的@MetroRSSReader應用

<common:VariableGridView.ItemsPanel> 
       <ItemsPanelTemplate> 
        <VariableSizedWrapGrid ItemWidth="225" 
              ItemHeight="{Binding ElementName=bounds, Path=Text}" 
              MaximumRowsOrColumns="5" Orientation="Vertical" 
              /> 
       </ItemsPanelTemplate> 
       </common:VariableGridView.ItemsPanel> 
     </common:VariableGridView> 

通知的ItemHeight值綁定到一個TextBlock

<TextBlock x:Name="bounds" Grid.Row="1" Margin="316,8,0,33" Visibility="Collapsed"/> 

這是在LayoutAwarePage.cs設置

public string Fix_item_height_for_current_screen_resolution() 
    { 
     var screenheight = CoreWindow.GetForCurrentThread().Bounds.Height; 
     var itemHeight = screenheight < 1000 ? "100" : "140"; 

     return itemHeight; 
    } 

您可以瀏覽完整的源代碼http://metrorssreader.codeplex.com/SourceControl/changeset/view/18233#265970

0

要使用VariableSizeWrapGrid你應該創建自己的GridView自定義控件並重寫PrepareContainerForItemOverride並設置元素該方法中的RowSpan和ColumnSpan。這樣每個元素將有它自己的高度/寬度。

這裏是一個很好的教程/傑裏·尼克松穿行:http://dotnet.dzone.com/articles/windows-8-beauty-tip-using

+1

任何想法如何添加這樣的行/列跨度可以在模型更改時獲得數據綁定和更新。 PrepareContainerForItemOverride不會在更改上調用。 – 2012-11-19 19:43:57

0

託管今天推測這一個。您需要使用WinRT XAML Toolkit中的VisualTreeHelperExtension.cs(http://winrtxamltoolkit.codeplex.com)。對我來說,我試圖調整一個有GridView作爲其ItemsPanelTemplate的ListView,這個概念應該適用於你。

1)安裝到您的ListView的LayoutUpdated事件(這是當你將要更新的大小)

_myList.LayoutUpdated += _myList_LayoutUpdated;

2)使用VisualTreeHelperExtensions.GetDescendantsOfType()找到您的項目的數據模板中的常見(和唯一)元素類型(例如:寬度爲動態的TextBlock):

var items = VisualTreeHelperExtensions.GetDescendantsOfType<TextBlock>(_myList); 

if (items == null || items.Count() == 0) 
    return; 

3)獲取發現項目的最大寬度:

double maxWidth = items.Max(i => i.ActualWidth) + 8;

4)使用VisualTreeHelperExtensions。GetDescendantsOfType()找到主WrapGrid容器爲您的ListView:

var wg = _categoryList.GetDescendantsOfType<WrapGrid>(); 
if (wg == null || wg.Count() != 1) 
    throw new InvalidOperationException("Couldn't find main ListView container"); 

5)設置WrapGrid的ItemWidth到maxWidth你計算:

wg.First().ItemWidth = maxWidth;