Leandro Bardelli
Leandro Bardelli

Reputation: 11578

Filling a ComboBox with LINQ on Silverlight (WPF)

Im trying to load a XML into an object with LinQ in Silverlight with WPF, but i can't fill or binding my combobox.

The code of the object is:

    public class Language_Index
{
    public string Prefix { get; set; }
    public string Status { get; set; }
    public string Name { get; set; }
}

My XML is:

<languages_index>
<item prefix="VBNET" name="Visual Basic .NET" status="enabled" />
<item prefix="CS" name="C#" status="disabled" />

and the Code to load and present data is:

            string XmlString = e.Result; // Got all your XML data in to a string 

        XDocument elem = XDocument.Load(XmlReader.Create(new StringReader(XmlString)));

        var feed_language_index = from nod in elem.Descendants("languages_index")
                                  select new Language_Index
                                  {
                                      Name = nod.Element("item").Attribute("name").Value,
                                      Status = nod.Element("item").Attribute("status").Value,
                                      Prefix = nod.Element("item").Attribute("prefix").Value

                                  };

        LanguageSelector.ItemsSource = feed_language_index;

Of course the pasted code has no errors, but doesnt work. LanguageSelector its my combo. There is NO problem in the linq, the problem is when i binding the combobox :)

That i really want is try to do something like: MyComboBox.DataSource = MyClass.name; for every element.

thanks in advance! :)

Upvotes: 0

Views: 809

Answers (1)

Filip Skakun
Filip Skakun

Reputation: 31724

I'd suggest you try converting your LINQ to a list and check your VS output window for binding errors.

    string XmlString = e.Result;

    XDocument elem = XDocument.Load(XmlReader.Create(new StringReader(XmlString)));

    var feedLanguages = 
            (from nod in elem.Descendants("languages_index")
            select new Language_Index
            {
                    Name = nod.Element("item").Attribute("name").Value,
                    Status = nod.Element("item").Attribute("status").Value,
                    Prefix = nod.Element("item").Attribute("prefix").Value
            }).ToList();

    LanguageSelector.ItemsSource = feedLanguages;

Sorry for removing your underscores. :)

Upvotes: 1

Related Questions