2010-04-16 67 views
2

我想讓我的數據在GridLayout中正確顯示,GridLayout將用作ListBox中Item的DataTemplate。下面是我在做什麼相關的代碼:在TextBox中使用TextWraping,在Grid中用於在WP7中的ListBox中使用

<Grid Name="FeedItemTemplate"> 
    <Grid.RowDefinitions> 
    <RowDefinition Height="Auto" /> 
    <RowDefinition Height="Auto" /> 
</Grid.RowDefinitions> 

<Grid.ColumnDefinitions> 
    <ColumnDefinition Width="Auto" /> 
    <ColumnDefinition Width="*" /> 
    <ColumnDefinition Width="Auto" /> 
</Grid.ColumnDefinitions> 

<Image Source="{Binding ProfileImage}" Grid.RowSpan="2" Height="75" Width="75" VerticalAlignment="Center" Margin="1" /> 
<TextBlock Text="{Binding UserName}" Grid.Column="1" Foreground="#FFC8AB14" FontSize="28" HorizontalAlignment="Left"/> 
<TextBlock Text="{Binding TimeStamp}" Grid.Column="2" TextWrapping="Wrap" FontSize="18" HorizontalAlignment="Center"/> 
<TextBlock Text="{Binding Message}" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" TextWrapping="Wrap" FontSize="24" /> 
</Grid> 

的問題是,採用這種佈局,當TextWrapping設置爲Wrap,顯示正確的項目,而是通過列表框一切滾動的時候真的是戰戰兢兢,你不能以小增量滾動,它只是跳到所有的地方。

爲什麼要這樣做?正如我所說的,只有當TextWrapping設置爲Wrap時,它才能做到這一點。當它沒有被使用時,它可以很好地滾動,但文本全部沿着一條線離開屏幕。

回答

1

如果您明確將頂層柵格元素的寬度設置爲固定大小,它會不會繼續跳躍?

1

出於某種原因,我不完全瞭解,設置ListBox的ItemsPanel屬性爲一個StackPanel可以解決你的問題:

<UserControl.Resources> 
    <ItemsPanelTemplate x:Key="MyItemsPanelTemplate"> 
     <StackPanel/> 
    </ItemsPanelTemplate> 
</UserControl.Resources> 

... 

<ListBox ... ItemsPanel="{StaticResource MyItemsPanelTemplate}"/> 
+0

這是因爲ItemsPanel在默認情況下使用了VirtualizedStackPanel,它往往不像普通的StackPanel那樣執行,因爲它需要在後臺的可視樹中創建和刪除項目。請記住,如果更改爲StackPanel,則當您向列表中添加更多項目時,內存使用量將會增加,因爲未顯示的項目不會從可視化樹中刪除。 – 2011-04-01 03:28:14

0

這是列表框在當前CTP滾動,當你有可變的已知問題高度物品。目前的解決方法是在列表框項目內容上設置一個固定的高度。您可能還會注意到滾動條並不會一直處於最佳狀態。解決方法也修復了這一問題。

Reference