我有一个列表选择器,它由XML文件中的两个字符串传播,一个是名称,另一个是值。
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;
}
}
我可以通过使用来读取值OR名称
((appname.pagename.CustomValue)(this.Listpicker.SelectedItem)).StringValue
但我无法设置selectedItem,如果我使用类似于上面的方法,它会更改类CustomValue中StringValue的值。
非常感谢任何帮助!
谢谢:)
答案 0 :(得分:4)
您无法设置SelectedItem = "something"
,因为该集合包含CustomValue
而不是string
的实例。您必须将所选项目作为可用项目之一。
假设您想要选择收藏中的第一个项目。有两种方法可以做到这一点:
Listpicker.SelectedItem = stringNames.First();
或
Listpicker.SelectedIndex = 0;