2014-03-24 173 views
1

我有一個ItemsControl項目。我想要像圖片中那樣排列物品。如果有更多的元素,該空間應該有一個水平滾動條。我如何歸檔這個?滾動條ItemsControl垂直填充,然後水平滾動

enter image description here 相關代碼:

<ItemsControl.Template> 
    <ControlTemplate TargetType="ItemsControl"> 
     <ScrollViewer PanningMode="Both" > 
     <ItemsPresenter /> 
     </ScrollViewer> 
     </ControlTemplate> 
    </ItemsControl.Template> 
    <ItemsControl.ItemsPanel> 
    <ItemsPanelTemplate> 
     <WrapPanel /> 
    </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 

編輯: 我就是報道說,建議的解決方案沒有工作對不起,原來該項目的高度是在運行時動態改變,導致只有一個項目垂直的空間。

回答

1

會是這樣的工作?

<ItemsControl.Template> 
    <ControlTemplate TargetType="ItemsControl"> 
     <ScrollViewer HorizontalScrollBarVisibility="Auto" 
      VerticalScrollBarVisibility="Disabled" > 

,如果這不工作,試試這個

<ItemsControl.ItemsPanel> 
    <ItemsPanelTemplate> 
     <WrapPanel Height="{Binding RelativeSource={RelativeSource 
      Mode=FindAncestor, AncestorType={x:Type ScrollViewer}}, 
      Path=ActualHeight}" /> 

編輯

別忘了讓你的WrapPanel Orientation="Vertical"

1

示例代碼爲您的問題

<ItemsControl.Template> 
       <ControlTemplate> 
       <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Hidden" Height="100" Width="200"> 
         <ItemsPresenter /> 
        </ScrollViewer> 
       </ControlTemplate> 
      </ItemsControl.Template> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <WrapPanel Orientation="Vertical" Height="100"/> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 

希望這是你正在尋找的東西!

+0

按預期這是行不通的。我更新了我的照片以澄清問題。 – Florian

+0

將固定高度設置爲WrapPanel並將方向設置爲垂直並將高度和寬度固定爲ScrollViewer後,它不工作? – user1246682

1
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" > 
    <ItemsControl> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <WrapPanel Orientation="Vertical" /> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
    </ItemsControl> 
</ScrollViewer> 

憑藉其列表框中簡單:

<ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled" > 
    <ListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <WrapPanel Orientation="Vertical" /> 
     </ItemsPanelTemplate> 
    </ListBox.ItemsPanel> 
</ListBox>