Hey I need to access items in my listbox I've made in WPF. What I have right now is the user can add a new class object to ObservableCollection which the ListBox.ItemsSource is bound to. This class has data in it where that data is used in things like a combobox, a text box and other simple things like that.
What i've made is really based off of one of the tut videos from windowsclient.net
//Here's What I have so far
<ListBox x:Name="MyList" Width = "300" Height = 300">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock x:Name="ItemName"
Text={Binding Path=PersonName}/>
<ComboBox x:Name="NumberOfRes"
SelectedIndex="0"
SelectionChanged="NumberOfRes_SelectionChanged"
ItemsSource={Binding Path=ListOfNums}/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
//ListOfNums
private ObservableCollection<int> _listofNums =
new ObservableCollection<int>()
{ 1,2,3,4,5,6,7,8 };
public ObservableCollection<int> ListOfNums
{
get{return _listOfNums;}
}
//class Resident
public class Resident
{
public string personName {get;set;}
}
So I have that working. I have a button that adds a new class object and each object has it's data set properly through data binding. So I can see my default personName when a new object is added and I have a comboBox filled with integers from ListOfNums.
My propblem though is that I don't know how to access what these 'sub objects' (forgive me I'm new to WPF and don't know the proper term) are selecting. I want to know what my comboBox's SelectedItem is when a selectionChange event is raised or what my new personName is set to when the user types something into the textBox. So a user selects one of these ItemTemplate objects in the listBox and this item has a combobox and other UI controls that the user clicks. When the user clicks on one of these UI controls some data changes.
I've tried ((Resident)MyList.SelectedItem).personName
and a couple other methods just to see if I can access the data and manipulate it. Now the funny thing is that my comboBox in the ItemTemplate has a selection changed event that is called when the selection is changed, but I can't access the data!
Thanks in advance!