0

我的列表视图不显示绑定到它的数据以寻求帮助我在这里给出了仅显示一个元素的示例我想知道我的代码的问题在哪里请帮助我:(

这是我的代码:

<ListView ItemsSource="{Binding Path=MovieData}">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=MovieId}" Header="Movie Id" />
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Title}" Header="Title" />
                    <!-- Can Bind to complex subclass -->
                    <GridViewColumn Header="Movie(s)">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <ItemsControl ItemsSource="{Binding Path=MovieImage}">
                                        <ItemsControl.ItemsPanel>
                                            <ItemsPanelTemplate>
                                                <WrapPanel Orientation="Horizontal"/>
                                            </ItemsPanelTemplate>
                                        </ItemsControl.ItemsPanel>
                                    </ItemsControl>
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <!-- Binding with data triggers -->

                    <!-- Simple binding -->
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Recommendation}" Header="Recommendations" />

                </GridView>
            </ListView.View>
        </ListView>

这是我的班级电影:

public class Movie
    {
        public int MovieId { get; set; }
        public string Title { get; set; }
        public BitmapImage MovieImage { get; set; }
        public string Recommendation { get; set; }
        public Movie() { }

        public void CreateMovie(int i, string T , string R)
        {
            MovieId = i;
            Title = T;
            Recommendation = R;
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.UriSource = new        Uri(@"http://pierre.chachatelier.fr/programmation/images/mozodojo-original-image.jpg");
            bi.EndInit();
            MovieImage = bi;


        }

    }

这里我只添加一个元素进行测试:

 public List<Movie> MovieData= new List<Movie>();
 Movie m = new Movie();
 m.CreateMovie(5, "d", "s");
 MovieData.Add(m);
4

1 回答 1

0

如果您在后面的代码中使用视图模型方法:

public class MovieViewModel
{
   public ObservableCollection<Movie> MovieData { get; set; }

   public MovieViewModel()
   {
      //the code you post
      MovieData = new ObservableCollection<Movie>();
      Movie m = new Movie();
      m.CreateMovie(5, "d", "s");
      MovieData.Add(m);
   }
}

并在您的窗口构造函数中:

private MovieViewModel vm;
public MyWindow()
{
   InitializeComponent();
   vm = new MovieViewModel();
   this.DataContext = vm;
}

编辑:我刚刚阅读了您的评论。如果您的列表永远不会改变并且仅从数据库加载,您可以忽略我上面的答案,并且:

  • ItemsSource="{Binding Path=MovieData}"从 XAML 中的 ListView 中删除
  • 给 Listview 起个名字Name="movieListView"

将您的代码更改为以下内容:

List<Movie> MovieData= new List<Movie>();
Movie m = new Movie();
m.CreateMovie(5, "d", "s");
MovieData.Add(m);
movieListView.ItemsSource = MovieData;
于 2013-03-07T11:11:57.027 回答