2013-12-11 84 views
0

如果這是一個明顯的問題,我很抱歉,我對WPF相當陌生。我正在嘗試創建一個類似於下圖所示的「分組」列表框。隨着谷歌的幫助,我剛剛設法讓它工作,除了某些原因,我得到垂直滾動條可見。任何機構有任何想法嗎?這讓我瘋狂!由於在分組的WPF ListBox中缺少垂直滾動條

grouped ListBox example

我使用的代碼如下:

public class SampleData 
{ 
    public string Name { get; set; } 
    public string Group { get; set; } 

    public SampleData(string name, string group) 
    { 
     this.Name = name; 
     this.Group = group; 
    } 
} 


public partial class MainWindow : Window 
{ 
    private ObservableCollection<SampleData> _items = new ObservableCollection<SampleData>(); 

    public ObservableCollection<SampleData> Items 
    { 
     get { return _items; } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 

     _items.Add(new SampleData("Item1", "Group1")); 
     _items.Add(new SampleData("Item2", "Group1")); 
     _items.Add(new SampleData("Item3", "Group1")); 
     _items.Add(new SampleData("Item4", "Group1")); 
     _items.Add(new SampleData("Item5", "Group1")); 
     _items.Add(new SampleData("Item6", "Group1")); 

     _items.Add(new SampleData("Item7", "Group2")); 
     _items.Add(new SampleData("Item8", "Group2")); 
     _items.Add(new SampleData("Item9", "Group2")); 
     _items.Add(new SampleData("Item10", "Group2")); 
     _items.Add(new SampleData("Item11", "Group2")); 
     _items.Add(new SampleData("Item12", "Group2")); 
     _items.Add(new SampleData("Item13", "Group2")); 
     _items.Add(new SampleData("Item14", "Group2")); 
    } 
} 

,並在XAML,

<CollectionViewSource x:Key="groupedSampleData" Source="{Binding ElementName=main, Path=Items }"> 
    <CollectionViewSource.GroupDescriptions> 
     <PropertyGroupDescription PropertyName="Group" /> 
    </CollectionViewSource.GroupDescriptions> 
    </CollectionViewSource> 

    <Style x:Key="LBStyle" TargetType="{x:Type ListBox}"> 
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/> 
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
    <Setter Property="ScrollViewer.CanContentScroll" Value="True"/> 
    <Setter Property="ScrollViewer.PanningMode" Value="VerticalOnly"/> 
    </Style> 


<ListBox Grid.Row="0" Style="{StaticResource LBStyle}" ItemsSource="{Binding Source={StaticResource groupedSampleData}}" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="0" > 
    <ListBox.Template> 
    <ControlTemplate TargetType="{x:Type ListBox}"> 
     <ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Auto" PanningMode="VerticalOnly"> 
     <StackPanel> 
      <ItemsPresenter/> 
     </StackPanel> 
     </ScrollViewer> 
    </ControlTemplate> 
    </ListBox.Template> 

    <ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <StackPanel> 
     <Border Width="96" Height="96" Margin="4" Background="#DDD"/> 
     <TextBlock Margin="4,0,4,4" Text="{Binding Path=Name}" HorizontalAlignment="Center"/> 
     </StackPanel> 
    </DataTemplate> 
    </ItemsControl.ItemTemplate> 

    <ItemsControl.ItemsPanel> 
    <ItemsPanelTemplate> 
     <WrapPanel /> 
    </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 

    <ItemsControl.GroupStyle> 
    <GroupStyle> 
     <GroupStyle.Panel> 
     <ItemsPanelTemplate> 
      <VirtualizingStackPanel/> 
     </ItemsPanelTemplate> 
     </GroupStyle.Panel> 
     <GroupStyle.HeaderTemplate> 
     <DataTemplate> 
      <TextBlock Margin="4,16,4,4" FontWeight="Bold" FontSize="15" Text="{Binding Path=Name}"/> 
     </DataTemplate> 
     </GroupStyle.HeaderTemplate> 
    </GroupStyle> 
    </ItemsControl.GroupStyle> 
</ListBox> 

回答

1

刪除此

<ListBox.Template> 
    <ControlTemplate TargetType="{x:Type ListBox}"> 
     <ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Auto" PanningMode="VerticalOnly"> 
     <StackPanel> 
      <ItemsPresenter/> 
     </StackPanel> 
     </ScrollViewer> 
    </ControlTemplate> 
</ListBox.Template> 

列表框如果我看到的是ScrollViewer和StackPanel,爲什麼需要更改列表框的模板?然後你將重新定義ItemPanelTemplate爲一個WrapPanel。

<ItemsControl.ItemsPanel> 
    <ItemsPanelTemplate> 
     <WrapPanel /> 
    </ItemsPanelTemplate> 
</ItemsControl.ItemsPanel> 

有一個名爲ScrollViewer的靜態類,您可以在其中控制其滾動查看器的屬性。

<ListBox ScrollViewer.VerticalScrollBarVisibility="Auto"/> 
+0

嗨,獅子座,輝煌,感謝您的幫助,它修復了它。我也理解你的解釋:) – justdruit