问题
I have a list picker propagated by two strings from an XML file, one a name and one a value.
XmlReader xml = XmlReader.Create("file.xml");
XDocument _doc = XDocument.Load(xml);
var stringNames = from query in _doc.Descendants("string")
select new CustomValue
{
StringName = (string)query.Attribute("name"),
StringValue = (string)query.Attribute("value"),
};
Listpicker.ItemsSource = stringNames;
public class CustomValue
{
public string StringName
{
get;
set;
}
public string StringValue
{
get;
set;
}
}
I can read the value OR name by using
((appname.pagename.CustomValue)(this.Listpicker.SelectedItem)).StringValue
But i cannot set the selectedItem, if I used a method similar to the one above it changes the value of the StringValue in the class CustomValue.
Any help very much appreciated!
Thanks :)
回答1:
You can't set SelectedItem = "something" as the collection holds instances of CustomValue not string. You have to make the seleccted item one of the availabel items.
Let's say you wanted to selected the first item in your collection. There are 2 ways to do this:
Listpicker.SelectedItem = stringNames.First();
or
Listpicker.SelectedIndex = 0;
来源:https://stackoverflow.com/questions/5715549/wp7-listpicker-set-selecteditem-problem