0

Hello i am having some issues with data binding in silverlight for wp7. Basically i have trouble binding a list that is inside another list of objects. Here is the example of my classes.

public class Movie
    {

        public Movie()
        {
            _Venues = new ObservableCollection<Venue>();
        }

        private String _Title;

        public String Title
        {
            get { return _Title; }
            set { _Title = value; }
        }
        private String _Director;

        public String Director
        {
            get { return _Director; }
            set { _Director = value; }
        }
        private String _Runtime;

        public String Runtime
        {
            get { return _Runtime; }
            set { _Runtime = value; }
        }

        private ObservableCollection<Venue> _Venues;

        public ObservableCollection<Venue> Venues
        {
            get { return _Venues; }
            set { _Venues = value; }
        }

public class Venue
    {
        private String _Date;

        public String Date
        {
            get { return _Date; }
            set { _Date = value; }
        }
        private String _Time;

        public String Time
        {
            get { return _Time; }
            set { _Time = value; }
        }
    }
}

My intention is to set a values from Venues in xaml:

 <!-- The template for movie items -->
        <DataTemplate x:Key="moviesItemTemplate">
            <StackPanel Grid.Column="1"  VerticalAlignment="Top">
                <StackPanel Grid.Column="1"  VerticalAlignment="Top" Orientation="Horizontal">
                    <Image delay:LowProfileImageLoader.UriSource="{Binding ImageThumb}"/>
                    <TextBlock Text="{Binding Title}" FontSize="26"  Margin="12,-12,12,6" HorizontalAlignment="Center" VerticalAlignment="Center" />
                </StackPanel>
                <ListBox ItemsSource="{Binding Venues}">
                    <TextBlock Text="{Binding Date}" Foreground="Orange"  />
                </ListBox>
            </StackPanel>
        </DataTemplate>

 <!--ContentPanel contains details text. Place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <toolkit:LongListSelector x:Name="moviesListGropus" Background="Transparent" 
                 ItemTemplate="{StaticResource moviesItemTemplate}"
                 GroupHeaderTemplate="{StaticResource groupHeaderTemplate}"
                 GroupItemTemplate="{StaticResource groupItemTemplate}" >   
                <toolkit:LongListSelector.GroupItemsPanel>
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel/>
                    </ItemsPanelTemplate>
                </toolkit:LongListSelector.GroupItemsPanel>
            </toolkit:LongListSelector>
        </Grid>
    </Grid>

In cs code I used a manual loop to get movieGroups instead of a Linq expression but i think that should not be the problem here:

ObservableCollection<Group<Movie>> movieGroups = new ObservableCollection<Group<Movie>>();
this.moviesListGropus.ItemsSource = movieGroups;

public class Group<T> : IEnumerable<T>
        {
            public Group(string name, IEnumerable<T> items)
            {
                this.Title = name;
                this.Items = new List<T>(items);
            }

            public override bool Equals(object obj)
            {
                Group<T> that = obj as Group<T>;

                return (that != null) && (this.Title.Equals(that.Title));
            }

            public string Title
            {
                get;
                set;
            }

            public IList<T> Items
            {
                get;
                set;
            }

            #region IEnumerable<T> Members

            public IEnumerator<T> GetEnumerator()
            {
                return this.Items.GetEnumerator();
            }

            #endregion

            #region IEnumerable Members

            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return this.Items.GetEnumerator();
            }

            #endregion
        }

Sadly i cannot provide an Image due to stackoverflows restrictions but the result can be seen on http://postimage.org/image/as49vtqvn/

EDIT 2nd part: The idea is to put another textBlock inside the template which would be responsible for showing the desired Time from the list of Venues. For that i need to go 1 level deeper because venues is a list and i want a specific object from that list. Any suggestions?

 <DataTemplate x:Key="moviesItemTemplate">
            <StackPanel Grid.Column="1"  VerticalAlignment="Top">
                <StackPanel Grid.Column="1"  VerticalAlignment="Top" Orientation="Horizontal">
                    <Image delay:LowProfileImageLoader.UriSource="{Binding ImageThumb}"/>
                    <TextBlock Text="{Binding Title}" FontSize="26"  Margin="12,-12,12,6" HorizontalAlignment="Center" VerticalAlignment="Center" />
                </StackPanel>
                <ItemsControl x:Name="venueTime" ItemsSource="{Binding Path= Venue}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Time}" Foreground="Orange" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </DataTemplate>
4

1 回答 1

3

The problem is this:

<ListBox ItemsSource="{Binding Venues}">
    <TextBlock Text="{Binding Date}" Foreground="Orange"  />
</ListBox>

I suspect what you probably want is:

<ListBox ItemsSource="{Binding Venues}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Date}" Foreground="Orange" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The TextBlock you had aribtrarily inside the ListBox was being ignored because it wasn't valid at that point.
You need to specify how the items in the listbox should be templated if you don't want the default behaviour. The default behaviour just being a TextBlock with the text of the datacontext. Because you'd passed an object (not text) the framework was calling ToString() on it and by default this returns the object's Type and that is what you were seeing.

于 2012-05-23T09:24:10.717 回答