I will explain my problem in simplest way. I have been dealing with both listview and gridview and binding for a long time, but now I am having a unexplanable problem, so i really need some help.
Below is the xaml code of my listview.
<ListView Name="OtherVideosList" ItemsSource="{x:Bind VideoFiles}" SelectionChanged="OtherVideosList_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:VideoFile">
<StackPanel Orientation="Horizontal">
<Image Source="{x:Bind Thumbnail}"/>
<StackPanel>
<TextBlock Text="{x:Bind FileName}"/>
<TextBlock Text="{x:Bind Duration}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I am binding it to a ObservableCollection below is the datatype class for it.
public class VideoFile
{
public string FileName { get; set; }
public string Duration { get; set; }
public StorageFile File { get; set; }
public BitmapImage Thumbnail { get; set; }
}
using this class I am creating the item source
public ObservableCollection<VideoFile> VideoFiles { get; set; }
I use a button to open multiple files and then put them to item source, to play them later on the media element. below is the code for the event handler.
private async void OpenClick(object sender, RoutedEventArgs e)
{
try
{
var p = new FileOpenPicker();
foreach (var item in videoTypes)
{
p.FileTypeFilter.Add(item);
}
//Curentplayingfiles is IReadOnlyList<StorageFiles>
CurrentlyPlayingFiles = await p.PickMultipleFilesAsync();
if (CurrentlyPlayingFiles.Count != 0)
{
if (CurrentlyPlayingFiles.Count == 1)
{ //this if block works absolutely fine
CurrentlyPlayingFile = CurrentlyPlayingFiles[0];
var s = await CurrentlyPlayingFile.OpenReadAsync();
ME.SetSource(s, CurrentlyPlayingFile.ContentType);
}
else
{
VideoFiles = new ObservableCollection<VideoFile>();
foreach (var file in CurrentlyPlayingFiles)
{
//Thumbnail and GetDuration are my own static methods to get thumbnail
//and duration property of the file respectively
VideoFiles.Add(new VideoFile { Thumbnail = await Thumbnail(file), Duration = await GetDuration(file), File = file, FileName = file.DisplayName });
}
//exception occurs on this very line below, because here OtherVideosList has zero items.
OtherVideosList.SelectedIndex = 0;
}
}
}
catch (Exception s){ var dr = s.Message; }
}
I have mentioned the key points in the comments for you guyx. any help would be highly appreciated, thanks a lot..
It looks like in your code you are bound to an observable collection videofiles. Do not set it to new before adding the items. If the collection is already bound this will break your binding. Clear all the items from the list instead