WP7 ListPicker Set SelectedItem问题

时间:2011-04-19 11:21:35

标签: c# xml silverlight windows-phone-7

我有一个列表选择器,它由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的值。

非常感谢任何帮助!

谢谢:)

1 个答案:

答案 0 :(得分:4)

您无法设置SelectedItem = "something",因为该集合包含CustomValue而不是string的实例。您必须将所选项目作为可用项目之一。

假设您想要选择收藏中的第一个项目。有两种方法可以做到这一点:

Listpicker.SelectedItem = stringNames.First();

Listpicker.SelectedIndex = 0;