WP7 ListPicker Set SelectedItem Problem

夙愿已清 提交于 2019-12-25 14:13:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!