我的listView不显示我的数据

本文关键字:我的 数据 显示 listView | 更新日期: 2023-09-27 18:02:38

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

下面是我的代码:
<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>

这是我的类Movie:

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;

        }
    }

这里我只添加了一个test元素:

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

我的listView不显示我的数据

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

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

EDIT:我刚看了你的评论。如果你的列表永远不会改变,它只从数据库加载,你可以忽略我上面的答案:

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

将你的代码修改如下:

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