0
如果這是一個明顯的問題,我很抱歉,我對WPF相當陌生。我正在嘗試創建一個類似於下圖所示的「分組」列表框。隨着谷歌的幫助,我剛剛設法讓它工作,除了某些原因,我得到垂直滾動條可見。任何機構有任何想法嗎?這讓我瘋狂!由於在分組的WPF ListBox中缺少垂直滾動條
我使用的代碼如下:
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>
嗨,獅子座,輝煌,感謝您的幫助,它修復了它。我也理解你的解釋:) – justdruit