2010-03-22 45 views
3

我有一個ItemsControl它從上到下罷了,但我無法得到它的子項佔據的ItemsControl的整個寬度:的Silverlight:設定項目在寬度ItemsControl中以伸展

alt text

我基本上需要拉伸綠色的位來填充控件的寬度(如藍色位所示)。

我已經嘗試過將模板項目的HorizontalAlignment屬性設置爲Stretch,我甚至嘗試將它的Width屬性綁定到ItemsControl寬度,但都沒有工作。

應該是直截了當的,但它的東西,我不能完全弄清楚...

編輯:這裏的ItemTemplate中(整個事情是它本身包含這勢必會一個ItemsControl ItemTemplate模板子對象的列表):

<DataTemplate> 
    <Border CornerRadius="5" Background="#ddd"> 
     <StackPanel> 
      <TextBlock Text="{Binding Name}" FontSize="18" Foreground="#bbb"/> 
      <ItemsControl ItemsSource="{Binding PlugIns}"> 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <toolkit:WrapPanel HorizontalAlignment="Stretch" /> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Margin="10,0,10,10" Tag="{Binding}" 
           MouseEnter="StackPanel_MouseEnter"> 
          <Border Child="{Binding Icon}" /> 
          <TextBlock Text="{Binding Name}" /> 
         </StackPanel> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 
     </StackPanel> 
    </Border> 
</DataTemplate> 
+0

您的ItemTemplate會有幫助嗎? – AnthonyWJones 2010-03-22 11:29:52

+0

好吧現在我很困惑,當你只想要一個直線列表時,爲什麼要使用WrapPanel? – AnthonyWJones 2010-03-22 11:44:24

+0

這是爲了包含n個需要包裝的項目的子項ItemsControl。 – 2010-03-22 11:46:55

回答

1

好的,爲了繼續構建應用程序,我想出瞭如何解決這個問題。本質上,當我更改ItemsControl的模板以支持滾動時,項目橫跨水平填充:)

<ItemsControl> 
    <ItemsControl.Template> 
     <ControlTemplate> 
      <ScrollViewer Padding="{TemplateBinding Padding}"> 
       <ItemsPresenter /> 
      </ScrollViewer> 
     </ControlTemplate> 
    </ItemsControl.Template> 
    ... 
</ItemsControl> 
3

您需要配置ListBoxItemHorizontalContentAlignment,您可以通過在ListBoxItemContainerStyle使用Style對象做到這一點,如下所示: -

<ListBox ....> 
    <ListBox.ItemContainerStyle> 
    <Style TargetType="ListBoxItem"> 
     <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
    </Style> 
    </ListBox.ItemContainerStyle> 
+0

謝謝,但我真的不想使用ListBox,因爲不需要選擇 – 2010-03-22 11:54:18

相關問題