title: Why do I get the error: " Specified Cast is not valid" when trying to populate my listview in xamarin.forms with a list in my viewModel?
body: <p>This is the code behind of the main page in my xamarin app</p>
  <pre><code>    public partial class MainPage : ContentPage
      {
      PenStockVM&lt;PenStock&gt; penstocks;
      public MainPage()
      {
          InitializeComponent();
  
          penstocks = new PenStockVM&lt;PenStock&gt;();
          
          //I get the error at this point saying that this cast is invalid. How to fix this?
          listView.ItemsSource = (System.Collections.IEnumerable)penstocks;
  
          BindingContext = penstocks;
      }
  }
  </code></pre>
  <p>This is my Penstock ViewModel class:</p>
  <pre><code>       class PenStockVM&lt;Penstock&gt;
      {
      public List&lt;PenStock&gt;PenStocks { get; set; }
  
      public PenStockVM()
      {
          PenStocks = new List&lt;PenStock&gt;();
          FillPenStockData();
      }
  
      private ObservableCollection&lt;Graph&gt; FillGraphData()
      {
          ObservableCollection&lt;Graph&gt; entries = new ObservableCollection&lt;Graph&gt;();
          Random random = new Random();
  
          for (int i = 0; i &lt; 10; i++)
          {
              entries.Add(new Graph { Time = i, Units = random.Next(100) });
          }
  
          return entries;
      }
  
      private void FillPenStockData()
      {
          PenStock object1 = new PenStock { Id = 1, Name = &quot;Main PenStock&quot;, Data = 
    FillGraphData(), Rotations = 100 };
          PenStocks.Add(object1);
  
          PenStock object2 = new PenStock { Id = 2, Name = &quot;Weir&quot;, Data = FillGraphData(), 
   Rotations = 100 };
          PenStocks.Add(object2);
  
           PenStock object3 = new PenStock { Id = 3, Name = &quot;Spillway&quot;, Data = FillGraphData(), 
  Rotations = 100 };
          PenStocks.Add(object3);
  
          PenStock object4 = new PenStock { Id = 4, Name = &quot;Sub PenStock 1&quot;, Data = 
  FillGraphData(), Rotations = 100 };
          PenStocks.Add(object4);
  
          PenStock object5 = new PenStock { Id = 5, Name = &quot;Sub PenStock 2&quot;, Data = 
  FillGraphData(), Rotations = 100 };
          PenStocks.Add(object5);
  
          PenStock object6 = new PenStock { Id = 6, Name = &quot;Sub PenStock 3&quot;, Data = 
  FillGraphData(), Rotations = 100 };
          PenStocks.Add(object6);
  
      }
  }
  </code></pre>
  <p>This is my PenStock model class:</p>
  <pre><code>      public class PenStock
      {
      public int Id { get; set; }
      public string Name { get; set; }
      public ObservableCollection&lt;Graph&gt; Data { get; set; }
      public int Rotations { get; set; }
   }
  </code></pre>
  <p>This is my other Graph model class:</p>
  <pre><code>       public class Graph: INotifyPropertyChanged
    {
     private int time;
    private int units;
  
    public event PropertyChangedEventHandler PropertyChanged;
  
  public int Time
  {
      get { return time; }
  
      set
      {
          time = value;
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(&quot;Time&quot;));
      }
    }
  
    public int Units
     {
      get { return units; }
  
      set
      {
          units = value;
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(&quot;Time&quot;));
      }
     }
  }
  </code></pre>
  <p>Do I get this error because I am trying to cast a generic list and the listView only accepts non-generic collections as item source? I am new to xamarin and please help me! Thank you.</p>