1

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

5
  • what are director[0] and actor[0] supposed to represent? IF they are external to your query, they will be the same value no matter how many elements are in the list. I think your xml sample may be incomplete. Also, you seem to be using a combination of data binding and assigning values in the codebehind. Commented Mar 12, 2012 at 14:35
  • Will edit to add the full XML code, director[0] actor[0] represnt the names of the director/actor from the name attribute from the xml Commented Mar 12, 2012 at 14:48
  • The xml still looks incomplete. If I ad a <movies> and <movie> tag at the top, it makes it valid XML, but it is only one movie. Is that correct? Commented Mar 12, 2012 at 15:47
  • Ok, let me see if I can ask one more clarifying question. Do you expect to see a list of items where you have bound castDirector and castActor? - essentially a treeview? Commented Mar 12, 2012 at 15:57
  • swear i cant copy and paste today :/ will try edit it again.. currently i see just the the first array item when i castDirector = director[0], castActor = actor[0], when i change it to [1] the second actor is displayed :/ what i would like to do is display all the actors Commented Mar 12, 2012 at 16:01

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Worked :) thank you! this method makes more sense. one quick thing i added this line: ActorPic = x.Descendants("person").Where(p => p.Attribute("job").Value == "Actor").Select(a => a.Attribute("thumb").Value).ToList() to spit out the url for the actor pics, which in the XAML shows up correctly via the <ListBox ItemsSource="{Binding Actors}" Margin="5"/> but when i change it to: <Image Source="{Binding ActorPic}" Margin="5" Height="100" Width="100" /> nothing shows up :/ am i missing something really obvious? thanks again
ActorPic is a List<string> - you can't bind one image to a List. You will need another ItemsControl in there that you bind Actor to, then have an ItemTemplate that displays the picture.
0

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?

1 Comment

you're right i had them ItemsSource twice, thanks for picking that up, adjusting the height still only dsiplayed the first element

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.