Click here to Skip to main content
11,796,275 members (77,886 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# WPF User-controls

I'm new to WPF and C#. I've tried to seach the web for a simple tutorial or code on how I can capture the list of all the selected items in a listbox.

I am doing the following:

1. Have a List<ABC> where A has two properties: Name and Address.
2. I am using DataBinding to populate a ListBox by the following:


<ListView Name="AddData" ItemsSource="{Binding}" DisplayMemberPath="Name" />


AddData.DataContext = XClass.getMyABCList(); // This returns the List<ABC>

This all works as I see the the Name property values in the ListBox.

Now, I want to select 3 of the items in the list. When I click on a "Done" button, I want to get the following information:

1) The indexes of the selected items
2) The Name values of the selected items
3) The Address values of the selected items.

I can obtain (2) and (3) if I can get (1) as I can cross reference the master List

Would appreciate any help in this.


Posted 20-Jun-11 5:38am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Rather than attempting to do this in a Win Forms manner, you can achieve this in a WPF-centric way by adding an IsSelected property to the class behind the list. Then you bind to this property using the following:
  <Style TargetType="{x:Type ListBoxItem}">
     <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
Now your underlying list will have the IsSelected boolean set as appropriate, and you can just select the items where that value is set.
Member 8008832 at 20-Jun-11 12:43pm
Dude, Thanks a lot for this. I wouldn't have figured this out given that I am new to WPF and C#.

Can you kindly recommend any books or references on learning material....this would help greatly....Thanks again!!!
Pete O'Hanlon at 21-Jun-11 3:57am
Don't worry about it mate - we all had to start somewhere, and this stuff isn't obvious when you first start off. As for books, I'd recommend WPF Unleashed by Adam Nathan and Pro WPF in C# 2010 by Matthew MacDonald. Welcome to the world of WPF - it's one wild ride.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 460
1 Abhinav S 368
2 OriginalGriff 340
3 CPallini 269
4 Sergey Alexandrovich Kryukov 252
0 OriginalGriff 2,012
1 Maciej Los 1,615
2 KrunalRohit 1,310
3 CPallini 1,015
4 Richard MacCutchan 828

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 20 Jun 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100