我的列表视图不会显示绑定到它的数据 请求帮助我在这里给出了仅显示一个元素的例子 我想知道我的代码有什么问题 pleese帮助我:(
这是我的代码:
<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);
答案 0 :(得分:0)
如果您在代码中使用View Model方法:
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 Name="movieListView"
将您的代码更改为以下内容:
List<Movie> MovieData= new List<Movie>();
Movie m = new Movie();
m.CreateMovie(5, "d", "s");
MovieData.Add(m);
movieListView.ItemsSource = MovieData;