2013-11-15 25 views
1

我正在使用WrapPanel(具有垂直方向)作爲ItemsControl的項目面板。沒有添加組風格,項目可以正常變形。當我爲項目控件添加組風格時,wrappanel不會包裝項目,所有項目都會在單個列中垂直顯示,而不是跨越多個列。我沒有爲組頁眉或ItemTemplate使用任何複雜的佈局。WPF Group Style令人不安的包裝面板包裝

這裏是XAML源..

 <Grid> 
      <ItemsControl ItemsSource="{Binding Items}" Grid.Row="0" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Visible"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <WrapPanel Orientation="Vertical" 
         MaxHeight="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Grid}},Path=ActualHeight}"/> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.GroupStyle> 
       <GroupStyle> 
        <GroupStyle.HeaderTemplate> 
         <DataTemplate> 
          <StackPanel> 
           <TextBlock FontSize="15" FontWeight="Bold" Text="{Binding Name}"/> 
          </StackPanel> 
         </DataTemplate> 
        </GroupStyle.HeaderTemplate> 
       </GroupStyle> 
      </ItemsControl.GroupStyle> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="*"></ColumnDefinition> 
          <ColumnDefinition Width="*"></ColumnDefinition> 
         </Grid.ColumnDefinitions> 
         <TextBlock Text="{Binding Name}"/> 
         <TextBox Text="{Binding Value}" Grid.Column="1" HorizontalAlignment="Right"></TextBox> 
        </Grid> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
</Grid> 

回答

2

最後找出問題所在。

當使用GroupStyle時,我們需要提到GroupStyle.Panel,否則將使用默認面板 這是一個堆棧面板。

聲明爲Wrappanel的ItemsPanel不會用作組項目佈局的主面板。

    <GroupStyle.Panel> 
         <ItemsPanelTemplate> 
          <WrapPanel Orientation="Vertical"></WrapPanel> 
         </ItemsPanelTemplate> 
        </GroupStyle.Panel> 
+0

,這如何幫助? –

+0

我正在使用wrappanel來封裝項目控件中的項目(請參閱我的問題中的ItemsControl.ItemsPanel部分。)。當我們使用組風格時,wpf將忽略項目控制面板並將使用組面板,因爲它需要實現組風格。所以在我的情況下,它忽略了我的wrappanle,並使用默認的panle作爲stackpanel的組風格 – Bathineni

0

當您設定GroupDescription作爲解釋here,您的項目將被分爲GroupItem收集和每個組將作爲孩子到垂直StackPanel。一個GroupItem內的項目將仍然使用WrapPanel已定義爲ItemsPanel,但因爲你的WrapPanelOrientation設置爲Vertical你是一個VerticalStackPanel的後裔(可提供無限的垂直可用空間)的佈局系統不換行因爲它具有所有需要以垂直方式顯示項目的空間。

希望它現在變得更加清晰,您可以調整您的解決方案,也許可以將WrapPanelOrientation更改爲Horizontal

0

下面的代碼(XAML)的作品在我的情況:

<ItemsControl.ItemsPanel> 
    <ItemsPanelTemplate> 
     <WrapPanel Orientation="Horizontal"/> 
    </ItemsPanelTemplate> 
</ItemsControl.ItemsPanel>