Click here to Skip to main content
11,647,748 members (69,784 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF User-controls
Hi, 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: XAML: <ListView Name="AddData" ItemsSource="{Binding}" DisplayMemberPath="Name" /> C#: 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. Thanks, Manish
Posted 20-Jun-11 5:38am

1 solution

Rate this: bad
good
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 IsSelectedproperty to the class behind the list. Then you bind to this property using the following:
<ListBox.ItemContainerStyle>
  <Style TargetType="{x:Type ListBoxItem}">
     <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
  </Style>
</ListBox.ItemContainerStyle>
Now your underlying list will have the IsSelectedboolean set as appropriate, and you can just select the items where that value is set.
  Permalink  
Comments
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 jyo.net 500
1 F-ES Sitecore 410
2 OriginalGriff 322
3 DamithSL 315
4 CPallini 250
0 OriginalGriff 1,342
1 jyo.net 994
2 DamithSL 971
3 Sergey Alexandrovich Kryukov 873
4 CPallini 795


Advertise | Privacy | Mobile
Web04 | 2.8.150804.3 | 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