I need to select an item from a list box that contains objects of my class. Here is my code:
the load event:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
XDocument loadedData = XDocument.Load("file.xml");
var data = from query in loadedData.Descendants("element")
select new myClass
{
First = (string)query.Element("first"),
Second = (string) query.Element("second")
};
List<myClass> d = data.ToList<myClass>();
myList = d;
myListBox.ItemsSource = data;
}
and then my button which is supposed to change the selected Item:
private void button1_Click(object sender, RoutedEventArgs e)
{
myListBox.SelectedItem = myList[100];
}
am I doing something wrong here?
myListisn’t the collection you’ve bound to the list.Either make
da wider scoped variable and refer to that inbutton1_clickor
store
datainmyListrather than a copy of it.