2011-03-31 11 views
3

我正在實現一個ListBox,其ItemsPanel是一個WrapPanel as per this answer,但有一個轉折:我的ItemsSource是分組 CollectionView。將GroupStyle應用於我的ListBox,該問題中顯示的包裝不起作用:組始終垂直顯示。分組的CollectionView可以水平顯示嗎?

Snoop荷蘭國際集團在我的應用程序,這裏的原因:

Snoop displaying WrapPanel in GroupItem

正如你所看到的,WrapPanel,定義爲我的列表框的ItemsPanelTemplate,出現在ItemsPresenter 每個GroupItem;一個隱含的,垂直定向的StackPanel(粉紅色框中的頂部項目)被創建來包含GroupItems本身。

有沒有辦法來覆蓋這種行爲,所以GroupItems被放置在一個WrapPanel?我需要重新模板整個ListBox嗎?

更新:爲了說明什麼,我居然和我的列表框和的CollectionView分組做,讓我發表一點XAML:

<Grid> 
    <ListBox ItemsSource="{Binding}"     
      ScrollViewer.VerticalScrollBarVisibility="Disabled" 
      SelectionMode="Multiple" 
      ItemContainerStyle="{StaticResource itemStyle}"> 
     <ListBox.GroupStyle> 
      <GroupStyle> 
       <GroupStyle.HeaderTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding Name}" FontWeight="Bold"/> 
        </DataTemplate> 
       </GroupStyle.HeaderTemplate> 
      </GroupStyle> 
     </ListBox.GroupStyle> 
     <ListBox.ItemTemplate> 
      <DataTemplate DataType="{x:Type WpfApplication1:Item}"> 
       <StackPanel Orientation="Vertical"> 
        <TextBlock Text="{Binding Name}" FontSize="10"/> 
        <TextBlock Text="{Binding Amount, StringFormat={}{0:C}}" FontSize="10"/> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <WrapPanel Orientation="Vertical"/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
    </ListBox> 
</Grid> 

GroupStyle是在它的心臟:如果您刪除它,GroupItems不會被渲染,並且WrapPanel(你可以在上面的截圖中看到GroupItem下面出現)出現在屏幕截圖中(StackPanel)98的位置。

+0

爲什麼你的WrapPanel的'Orientation'設置爲'垂直'如果你想要一個水平列表?我無法形象地看到你的目標是什麼。 – 2011-04-02 03:04:43

+0

@ H.B。如果將它設置爲「垂直」,但禁用「垂直」滾動條,則WrapPanel將在增加高度時抓取多餘的行,但只會將*項橫向移出屏幕。查看「多列列表框」的答案,我上面鏈接了一個很好的可視化我的意思。 – 2011-04-02 05:09:37

回答

8

只有在GroupStyle中定義了HeaderTemplate時,纔會出現此行爲。

它可以通過設置GroupStyle.Panel屬性包含一個WrapPanel進行修正:

<ListBox.GroupStyle> 
    <GroupStyle> 
     <GroupStyle.HeaderTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Name}" FontWeight="Bold"/> 
      </DataTemplate> 
     </GroupStyle.HeaderTemplate> 
     <GroupStyle.Panel> 
      <ItemsPanelTemplate> 
       <WrapPanel></WrapPanel> 
      </ItemsPanelTemplate> 
     </GroupStyle.Panel> 
    </GroupStyle> 
</ListBox.GroupStyle> 

它會是這個樣子:
enter image description here

+0

'GroupStyle.Panel'是失蹤的一塊!感謝您的幫助,Greg。 :) – 2011-04-05 18:14:33

0

你應該能夠取代從項目控制組樣式(列表框):

<ListBox.GroupStyle> 
    <Style /> 
<ListBox.GroupStyle/> 

或者,你也應該能夠基於組項目對象上創建一個DataTemplate:

<DataTemplate DataType="{x:Type GroupItem}"> 
    <Panel /> 
</DataTemplate> 
+1

感謝您的建議,但不會只是樣式/模板*每個GroupItem *,不會更改周圍的容器* GroupItems的集合? – 2011-03-31 19:17:40