Longlistselector Wp7绑定对象列表中的列表

时间:2012-05-23 07:23:15

标签: windows-phone-7 silverlight-toolkit

您好我在Silverlight for wp7中遇到了数据绑定问题。基本上我无法绑定另一个对象列表中的列表。这是我的课程的例子。

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; }
        }
    }
}

我的目的是在xaml中设置Venues的值:

 <!-- 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>

在cs代码中,我使用手动循环来获取movieGroups而不是Linq表达式,但我认为这不应该是问题:

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
        }

遗憾的是,由于stackoverflows限制,我无法提供图像,但结果可以在http://postimage.org/image/as49vtqvn/

上看到

编辑第二部分:想法是在模板中放置另一个textBlock,负责从场地列表中显示所需的时间。为此,我需要更深入1级,因为场地是一个列表,我想从该列表中的特定对象。有什么建议吗?

 <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>

1 个答案:

答案 0 :(得分:3)

问题在于:

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

我怀疑你可能想要的是:

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

TextBlock内部的ListBox被忽略了,因为它在那时无效。
如果不需要默认行为,则需要指定列表框中的项目应如何模板化。默认行为只是带有datacontext文本的TextBlock。因为你传递了一个对象(而不是文本),所以框架在它上面调用了ToString(),默认情况下它会返回对象的Type,这就是你所看到的。