[WPF] 仿Windows文件管理器,鼠标拖动选择ListBox列表项+自动分栏Canvas面板
效果图:
鼠标拖动选择ListBoxItem的单独演示:
使用方式:修改ListBox模板,使用自定义面板ListBoxDragSelectViewer替代ScrollViewer;ListBoxDragSelectViewer里面封装了ScrollViewer;
<ControlTemplate TargetType="{x:Type ListBox}"> <pp:ListBoxDragSelectViewer Background="{TemplateBinding Background}" ScrollViewer.HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" ScrollViewer.CanContentScroll="False"> <ItemsPresenter /> </pp:ListBoxDragSelectViewer> </ControlTemplate>
自动分栏效果单独演示:
使用方式:像ListBox一样绑定ItemsSource以及修改ItemTemplate;行列将根据实际大小分成接近正方形的小格;子项支持鼠标拖动,做了自动吸附效果;目前控件分割线效果还不理想;
<pp:AutoGridCanvas x:Name="agc" Margin="5" Background="LightGray" StrokeThickness="1" Stroke="Black"> <pp:AutoGridCanvas.ItemTemplate> <DataTemplate> <Border Background="LightCyan"> <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="36" Text="{Binding}" /> </Border> </DataTemplate> </pp:AutoGridCanvas.ItemTemplate> </pp:AutoGridCanvas>
代码已上传至GitHub:https://github.com/LowPlayer/PP.Wpf