|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis Article demos how to create a component that enable multiselect support for your Listview. BackgroundI was developing a filelist, I didn't take much attention to selection mode, as I thought setting <ListView SelectionMode="Multiple" .../>
My Design processWhen I start, I tried to find the position of each
After some Googling, I found a method named
and return child items in the specified rect. //Unselect all visible selected items no matter if it's current selected or not. VisualTreeHelper.HitTest(lvSender, UnselectHitTestFilterFunc, new HitTestResultCallback(SelectResultCallback), new GeometryHitTestParameters(new RectangleGeometry(unselectRect))); //Select all visible items in select region. VisualTreeHelper.HitTest(lvSender, SelectHitTestFilterFunc, new HitTestResultCallback(SelectResultCallback), new GeometryHitTestParameters(new RectangleGeometry(selectRect))); The last problem is to draw the preview rectangle, which is visible when selecting, as my knowledge is limited, all I can do is to draw it in background, please post if you have a better solution. (As you see, the preview rectangle cannot deal with Scrolling, I havent figured out how to obtain scrolling information yet) lvSender.Background = new DrawingBrush(
DrawRectangle(selectRect, lvSender.ActualWidth, lvSender.ActualHeight));Using the codeI have created a component for this operation, you can include the code in your project and set a Attached property to enable MultiSelect, you may have to set the ItemPanel Template so there is space for start dragging.<ListView cont:ListViewSelectionHelper.MultiSelect="True"
cont:ListViewSelectionHelper.PreviewDrag="True">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Margin="0,0,20,0" IsItemsHost="True" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.Items>
<TextBlock>Test1</TextBlock>
<TextBlock>Test2</TextBlock>
<TextBlock>Test3</TextBlock>
<TextBlock>Test4</TextBlock>
<TextBlock>Test5</TextBlock>
</ListView.Items>
</ListView>
Setting the MultiSelect will attach 4 events to your ListView : How it looks when MultiSelect on my filelist
History05-02-08 Initial version (hopefully not the last version)
|
||||||||||||||||||||||