2011-10-28 20 views
10
<Grid.ColumnDefinitions> 
    <ColumnDefinition Width="Auto"/> 
    <ColumnDefinition /> 
</Grid.ColumnDefinitions> 

<Grid.RowDefinitions> 
    <RowDefinition Height="Auto"/> 
    <RowDefinition/> 
    <RowDefinition Height="40"/> 
</Grid.RowDefinitions> 

<c:SearchTextBox Grid.ColumnSpan="2" .../> 

<ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1"> 
    <ListBox 
    ItemsSource="{Binding Categories}" 
    IsSynchronizedWithCurrentItem="True" 
    ... /> 
</ScrollViewer> 

<!-- Here is what I'm talking about:--> 
<ListBox ItemsSource="{Binding Products}" 
    IsSynchronizedWithCurrentItem="True" Grid.Column="1" Grid.Row="1"> 
    <ListBox.ItemsPanel> 
    <ItemsPanelTemplate> 
     <WrapPanel /> 
    </ItemsPanelTemplate> 
    </ListBox.ItemsPanel> 
</ListBox> 

我想要的是應該佈置右列中的項目以填充窗口寬度,然後創建一個新行,這正是WrapPanel的作用。
問題是,WrapPanel僅在一行中列出項目,顯示下方的水平滾動條,而所有項目在右側「隱藏」,超出窗口大小。WrapPanel在一條長水平線(顯示滾動條)中列出項目而不是包裝線

我該如何預防?

+0

如果您刪除了scrollviewer,會發生什麼情況?該項目仍然向右擴展到水平滾動? – dcarneiro

+0

@Daniel你讓我錯了。 ScrollViewer包裝第一個ListBox,其中有兩個ListBox代表主 - 細節場景。問題在於使用WrapPanel作爲其ItemsPanelTemplate的第二個ListBox。 – Shimmy

回答

15

您需要將第二個ListBox的水平滾動條禁用。

<ListBox ItemsSource="{Binding}" 
     ScrollViewer.HorizontalScrollBarVisibility="Disabled" ...> 
.... 
</ListBox> 

編輯

Addtionally,還有就是你使用的ScrollViewer第一個列表框原因是什麼?爲什麼我要問的是,ListBox內部已經有ScrollViewer,其中默認的可見性是自動的。

+4

爲了澄清爲什麼發生這種情況,如果'ListBox'啓用了水平滾動條(默認情況下它們是),那麼它會告訴它的孩子在無限的水平空間中測量自己。通過刪除滾動條,您可以強制「WrapPanel」使用有限寬度,因此它實際上會包裹。 –

+0

@SteveGreatrex:感謝您的評論。 –