[英]Missing vertical scrollbar in grouped WPF ListBox
抱歉,這是一個明顯的問題; 我是WPF的新手。 我正在嘗試創建一個類似於下圖的“分組”列表框。 在google的幫助下,我幾乎設法使其正常工作,但由於某種原因,我得到了垂直滾動條。 有沒有什么想法? 這真讓我發瘋! 謝謝
我使用的代碼如下:
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>
刪除這個
<ListBox.Template>
<ControlTemplate TargetType="{x:Type ListBox}">
<ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Auto" PanningMode="VerticalOnly">
<StackPanel>
<ItemsPresenter/>
</StackPanel>
</ScrollViewer>
</ControlTemplate>
</ListBox.Template>
列表框在模板內有其自己的默認scrollviewer,如果我看到的只是ScrollViewer和StackPanel,為什么還要更改列表框的模板? 然后,將ItemPanelTemplate重新定義為WrapPanel。
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
有一個稱為ScrollViewer的靜態類,您可以在其中控制其scrollviewer的屬性。
<ListBox ScrollViewer.VerticalScrollBarVisibility="Auto"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.