2012-07-17 44 views
0

我試圖使用WrapPanel和方向=「橫向」時,拉伸ListBoxItems:水平定向-ListBoxItems不會拉伸

<ListBox HorizontalContentAlignment="Stretch" ItemsSource="{Binding SomeCollection}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Border> 
       <!--Some Textboxes and Labels--> 
      </Border> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
    <ListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <WrapPanel Orientation="Horizontal"/> 
     </ItemsPanelTemplate> 
    </ListBox.ItemsPanel> 
</ListBox> 

如果我不使用WrapPanel將擴大到ListBoxItems匹配ListBox的大小。當我使用WrapPanel時,ListBoxItems具有最小寬度。

簡述:

我有兩個ListBoxItems列表水平定向:

A list with two Items

當我展開主窗口,列表框也將擴大,因爲我的Horizo​​ntalAlignment =「拉伸」,但ListBoxItems不會。 enter image description here

所以,我想是像下面的例子中,列表框,擴大ListBoxItems:

The same list after I expanded the Window

難道還有比列表框對於這種情況其他更好的控制? 請讓我知道,如果這不夠清楚。 感謝您的幫助。

+0

我不能嘗試了這一點,看看它是否是一個問題,但嘗試添加IsItemsHost =「true」添加到WrapPanel 。 [MSDN文檔](http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.isitemshost.aspx) – 2012-07-17 16:53:27

+0

@Wonko the Sane剛剛嘗試過,似乎沒有任何區別:( – Aris 2012-07-17 16:58:47

+0

也設置你的Horizo​​ntalAlignment =「Stretch」爲WrapPanel? – 2012-07-17 17:09:34

回答

0

您應該能夠使用UniformGridItemsPanel像這樣:

<ListBox HorizontalContentAlignment="Stretch" ItemsSource="{Binding SomeCollection}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Border> 
       <!--Some Textboxes and Labels--> 
      </Border> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
    <ListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <UniformGrid Rows="1" Columns="2" /> 
     </ItemsPanelTemplate> 
    </ListBox.ItemsPanel> 
</ListBox> 
2

你不能使用水平包裝面板,也期望元素水平拉伸,這是一個利益矛盾的矛盾。其實,如果你想任何類型拉伸WrapPanel可能不是正確的面板。

如果您希望它們在佔用所有水平空間的同時佔用相等的空間,則可以使用UniformGrid(將Rows設置爲1)。

+0

是否有任何其他方式,我可以實現水平方向而不使用WrapPanel(或我也有StackPanel嘗試),並拉伸ListBoxItems? – Aris 2012-07-17 17:59:30

+0

@Aris:所以你想要從左到右堆疊物品,你也想從左到右拉伸它們?這仍然是一個合乎邏輯的矛盾,它是不可能的,如果你水平放置它們,你必須有一些具體的寬度。 – 2012-07-17 20:07:59

+0

@Aris - 也許你想要完成的一個圖像會對你有所幫助。 – 2012-07-17 20:24:50