zeem
zeem

Reputation: 105

display array from XML c# Windows Phone 7 listbox

Hi all i seem to be stuck with this, i can get it display the first item in the array but not the whole list

 var actor = xmlInfo
         .Descendants("person")
            .Where(x => x.Attribute("job").Value == "Actor")
            .Select(x => x.Attribute("name").Value)
         .ToArray();

my listbox is the following

 ThirdListBox.ItemsSource = from info in xmlInfo.Descendants("movies")
                                   select new MovieInfo

                                   {
                                       tagline = info.Element("movie").Element("tagline").Value,
                                       castDirector = director[0],
                                       castActor = actor[0],
                                   };

xaml is

<ListBox Name="ThirdListBox" Margin="0,-24,-12,0" ItemsSource="{Binding Items}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,-12,0" Width="432" Height="800"  Background="#82101010">
                           <TextBlock Text="{Binding tagline}" TextWrapping="Wrap" FontSize="30" DataContext="{Binding}" />
                            <TextBlock Text="{Binding castDirector}" TextWrapping="Wrap" FontSize="30" DataContext="{Binding}" />
                            <TextBlock Text="{Binding castActor}" TextWrapping="Wrap" FontSize="30" DataContext="{Binding}" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

sample xml

<movies>
  <movie>
    <translated>true</translated>
    <adult>false</adult>
    <language>en</language>
    <original_name>Batman</original_name>
    <name>Batman</name>
    <alternative_name>Batman (1989)</alternative_name>
    <type>movie</type>
    <id>268</id>
    <imdb_id>tt0096895</imdb_id>
    <url>http://www.themoviedb.org/movie/268</url>
    <overview></overview>
    <votes>30</votes>
    <rating>8.1</rating>
    <tagline></tagline>
    <certification>PG-13</certification>
    <released>1989-06-23</released>
    <runtime>126</runtime>
    <budget>35000000</budget>
    <revenue>413200000</revenue>
    <homepage/>
    <trailer>http://www.youtube.com/watch?v=HlsM2_8u_mk</trailer>
    <categories></categories>
    <keywords></keywords>
    <studios></studios>
    <languages_spoken></languages_spoken>
    <countries></countries>
    <images></images>
    <cast>
      <person name="Tim Burton" character="" job="Director" id="510" thumb="http://cf2.imgobject.com/t/p/w185/oKucd7g8BpfWGw8bDRLkwxa7bT1.jpg" department="Directing" url="http://www.themoviedb.org/person/510" order="0" cast_id="1"/>
      <person name="Bob Kane" character="" job="Characters" id="3794" thumb="http://cf2.imgobject.com/t/p/w185/uh9ILSkz0iCQ8o1Ljnz6ubYMvOD.jpg" department="Writing" url="http://www.themoviedb.org/person/3794" order="0" cast_id="2"/>
      <person name="Anton Furst" character="" job="Production Design" id="3807" thumb="" department="Art" url="http://www.themoviedb.org/person/3807" order="0" cast_id="22"/>
      <person name="Peter Guber" character="" job="Producer" id="3804" thumb="" department="Production" url="http://www.themoviedb.org/person/3804" order="0" cast_id="16"/>
      <person name="Marion Dougherty" character="" job="Casting" id="3806" thumb="" department="Production" url="http://www.themoviedb.org/person/3806" order="0" cast_id="21"/>
      <person name="Ray Lovejoy" character="" job="Editor" id="244" thumb="" department="Editing" url="http://www.themoviedb.org/person/244" order="0" cast_id="20"/>
      <person name="Danny Elfman" character="" job="Original Music Composer" id="531" thumb="http://cf2.imgobject.com/t/p/w185/pWacZpYPos8io22nEiim7d3wp2j.jpg" department="Sound" url="http://www.themoviedb.org/person/531" order="0" cast_id="19"/>
      <person name="Roger Pratt" character="" job="Director of Photography" id="293" thumb="" department="Camera" url="http://www.themoviedb.org/person/293" order="0" cast_id="18"/>
      <person name="Jon Peters" character="" job="Producer" id="3805" thumb="" department="Production" url="http://www.themoviedb.org/person/3805" order="0" cast_id="17"/>
      <person name="Warren Skaaren" character="" job="Screenplay" id="913" thumb="" department="Writing" url="http://www.themoviedb.org/person/913" order="0" cast_id="3"/>
      <person name="Sam Hamm" character="" job="Screenplay" id="3795" thumb="" department="Writing" url="http://www.themoviedb.org/person/3795" order="0" cast_id="23"/>
      <person name="Michael Keaton" character="Batman" job="Actor" id="2232" thumb="http://cf2.imgobject.com/t/p/w185/jSG7yQxSsGzz1TbDjq0811W5LK4.jpg" department="Actors" url="http://www.themoviedb.org/person/2232" order="0" cast_id="4"/>
      <person name="Jack Nicholson" character="Joker" job="Actor" id="514" thumb="http://cf2.imgobject.com/t/p/w185/xr1kLWa28vOzdfzsdmxG74q9asp.jpg" department="Actors" url="http://www.themoviedb.org/person/514" order="1" cast_id="5"/>
      <person name="Kim Basinger" character="Vicki Vale" job="Actor" id="326" thumb="http://cf2.imgobject.com/t/p/w185/kNNGFpfEr71nDUfzdVxklamI5nH.jpg" department="Actors" url="http://www.themoviedb.org/person/326" order="2" cast_id="6"/>
      <person name="Michael Gough" character="Alfred" job="Actor" id="3796" thumb="http://cf2.imgobject.com/t/p/w185/lSADK0gjUtcq4B6zKIUwB3SofSP.jpg" department="Actors" url="http://www.themoviedb.org/person/3796" order="3" cast_id="7"/>
      <person name="Pat Hingle" character="Commissioner James Gordon" job="Actor" id="3798" thumb="http://cf2.imgobject.com/t/p/w185/wonbnphqVPITaC25uC698Du4tuj.jpg" department="Actors" url="http://www.themoviedb.org/person/3798" order="4" cast_id="8"/>
      <person name="Billy Dee Williams" character="Harvey Dent" job="Actor" id="3799" thumb="http://cf2.imgobject.com/t/p/w185/9kaLGHdD8UXvoI7W6z2hrGQyU2r.jpg" department="Actors" url="http://www.themoviedb.org/person/3799" order="5" cast_id="9"/>
      <person name="Jack Palance" character="Carl Grissom" job="Actor" id="3785" thumb="http://cf2.imgobject.com/t/p/w185/RKqKZ2rRDNkv0jaQscDARfRh9D.jpg" department="Actors" url="http://www.themoviedb.org/person/3785" order="6" cast_id="10"/>
      <person name="Jerry Hall" character="Alicia Grissom" job="Actor" id="3800" thumb="" department="Actors" url="http://www.themoviedb.org/person/3800" order="7" cast_id="11"/>
      <person name="Tracey Walter" character="Bob the Goon" job="Actor" id="3801" thumb="http://cf2.imgobject.com/t/p/w185/pMeImyAZ8fpQ32QyPO8g6W8gs13.jpg" department="Actors" url="http://www.themoviedb.org/person/3801" order="8" cast_id="12"/>
      <person name="Lee Wallace" character="Mayor" job="Actor" id="3802" thumb="" department="Actors" url="http://www.themoviedb.org/person/3802" order="9" cast_id="13"/>
      <person name="William Hootkins" character="Eckhardt" job="Actor" id="663" thumb="http://cf2.imgobject.com/t/p/w185/vrWs2YAhbLuq6YNcUa5IHGQGP7r.jpg" department="Actors" url="http://www.themoviedb.org/person/663" order="10" cast_id="14"/>
      <person name="Edwin Craig" character="Rotelli" job="Actor" id="3803" thumb="" department="Actors" url="http://www.themoviedb.org/person/3803" order="11" cast_id="15"/>
      <person name="Robert Wuhl" character="Alexander Knox" job="Actor" id="4040" thumb="http://cf2.imgobject.com/t/p/w185/7bboXtHrq8qOtQBdcZko3KJfFyH.jpg" department="Actors" url="http://www.themoviedb.org/person/4040" order="12" cast_id="24"/>
    </cast>
    <version>1242</version>
    <last_modified_at>2012-03-10 12:14:40 UTC</last_modified_at>
  </movie>
</movies>

so what i am looking to do is display all actors in my listbox

thanks

Upvotes: 1

Views: 1336

Answers (2)

Robaticus
Robaticus

Reputation: 23157

So, I think based on what you've said, you're going to have to change a few things. First off, if you want to display a list of lists, you're not going to be able to do it by binding to textboxes. Here's a way to set it up. I know this isn't exactly what you're asking, but it might get you in the right direction.

A couple caveats:

  1. Don't use a ListBox unless you need a ListBox. ItemsControl works great if you don't need to select an item from the list.

  2. I'm not 100% sure what you're trying to get, so this is my best guess at what you ultimately want to see. If it isn't quite there, let me know, and I'll see if I can tweak it some more.

In your DataContext's class (e.g. ViewModel):

    public ObservableCollection<Movie> Items { get; set; }

    public MainViewModel()
    {
        XDocument xmlInfo = XDocument.Load(@"c:\temp\Xmlfile1.xml");


        var items = xmlInfo.Descendants("movie")
                        .Select(x => new Movie
                                         {
                                             MovieName = x.Element("name").Value, 
                                             Actors = x.Descendants("person").Where(p => p.Attribute("job").Value == "Actor").Select(a=>a.Attribute("name").Value).ToList(),
                                             Directors = x.Descendants("person").Where(p => p.Attribute("job").Value == "Director").Select(a => a.Attribute("name").Value).ToList()
                                         });

        Items = new ObservableCollection<Movie>(items);



    }

Movie is defined as:

public class Movie
{
    public string MovieName { get; set; }
    public List<string> Actors { get; set; }
    public List<string> Directors { get; set; }

}

Then, the XAML is defined as:

    <ListBox ItemsSource="{Binding Items}">

        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding MovieName}" FontSize="32"/>
                    <StackPanel Orientation="Horizontal">
                        <ListBox ItemsSource="{Binding Actors}" Margin="5"/>
                        <ListBox ItemsSource="{Binding Directors}" Margin="5"/>
                    </StackPanel>
                </StackPanel>

            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Which yields:

enter image description here

Upvotes: 1

Stuart
Stuart

Reputation: 66882

The code looks like it should work - although there are a few too many {Binding} expressions and you shouldn't set ItemsSource in the C# and in the XAML...

If you are seeing the first item, then the problem could just be that your list items are very tall:

<StackPanel Margin="0,0,-12,0" Width="432" Height="800" 

Try removing the Height="800" attribute?

Upvotes: 0

Related Questions