2012-07-11 17 views
0

我想爲我的ListBox指定多個列,但是我的搜索技巧在這一個上失敗了。如何自定義ListBox的ItemsPanelTemplate的佈局?

如何修改ListBoxItemsPanelTemplate來自定義顯示的列?

編輯:忘了什麼我已經試過已經

我已經試過代碼

<ItemsPanelTemplate x:Key="ItemsPanelTemplate1"> 
     <UniformGrid Columns="3" /> 
    </ItemsPanelTemplate> 

除了我失去了垂直滾動條

+0

您希望修改'ItemsPanelTemplate'的哪些控件? – Rachel 2012-07-11 15:51:31

+0

一個'ListBox',雖然只是因爲這就是Blend爲我自動生成的東西...我想從一個網格列表中顯示項目,就像一個不太複雜的Zune專輯藝術視圖。 – humanstory 2012-07-11 15:53:44

+1

你確實需要ListBox的選擇能力嗎?如果沒有,我會建議切換到'ItemsControl',並且在我的博客上有一些[ItemsControl Examples](http://rachel53461.wordpress.com/2011/09/17/wpf-itemscontrol-example/)包括一個關於設置'ItemsPanelTemplate'的示例 – Rachel 2012-07-11 15:54:49

回答

0

它shoudln't其中一期工程這很難但取決於 - 如果您對每個項目使用網格或某種(如此)控件,並且您不介意列與其中的可變寬度項目不是同一寬度,則只需向ItemTemplate添加網格即可做。

<ItemTemplate> 
    <DataTemplate> 
     <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition> 
       <ColumnDefinition> 
       <ColumnDefinition> 
      </Grid.ColumnDefinitions> 
      <SomeControl Grid.Column="0" /> 
      <SomeControl Grid.Column="1" /> 
      <SomeControl Grid.Column="2" /> 
     </Grid> 
    </DataTemplate> 
</ItemTemplate> 

唯一的問題是,如果你想在格列所有相同的大小與大小可變的內容 - 在這種情況下,這將是一個涉及多一點 - 否則,你可以明確地設置大小,或讓內容通過將列寬設置爲"Auto"來決定大小。

0

下面是我認爲你在尋找的一個簡單示例,包括3列,項目環繞和自動垂直滾動,它們將根據周圍的佈局工作。

<ListBox HorizontalContentAlignment="Stretch"> 
    <ListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <UniformGrid Columns="3"/> 
     </ItemsPanelTemplate> 
    </ListBox.ItemsPanel> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Border MinHeight="150" Margin="5" Background="Green" CornerRadius="4"> 
       <TextBlock Text="{Binding}" Foreground="White"/> 
      </Border> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 

    <System:String>One</System:String> 
    <System:String>Two</System:String> 
    <System:String>Three</System:String> 
    <System:String>Four</System:String> 
    <System:String>Five</System:String> 
    <System:String>Six</System:String> 
    <System:String>Seven</System:String> 
    <System:String>Eight</System:String> 
    <System:String>Nine</System:String> 
    <System:String>Ten</System:String> 
</ListBox>