title: C# WPF Iterate through ListView items
body: <p>I've been going nuts trying to find a solution to what seems like a simple problem. I am trying to find a way to Iterate through my ListView and get values.</p>
  
  <p>I have a ListView with bindings which looks like this in xaml (simplified):</p>
  
  <pre><code>&lt;ListView x:Name="MColumnsListXaml" HorizontalAlignment="Left" Height="Auto" Margin="0,42,0,0" VerticalAlignment="Top" Width="Auto"&gt;
      &lt;ListView.View&gt;
          &lt;GridView&gt;
              &lt;GridViewColumn Header="First Name" Width="Auto" DisplayMemberBinding="{Binding MColumnName}" /&gt;
              &lt;GridViewColumn Header="Last Name" Width="Auto" DisplayMemberBinding="{Binding MColumnName2}" /&gt;
          &lt;/GridView&gt;
      &lt;/ListView.View&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>I declare bindings:</p>
  
  <pre><code>public class MandatoryColumns
  {
      public string MColumnName { get; set; }
      public string MColumnName2 { get; set; }
  }
  </code></pre>
  
  <p>Columns are populated by an event trigger which calls a method:</p>
  
  <pre><code>private void btnValidateColumns_Click(object sender, RoutedEventArgs e)
  {
      MandatoryOptionalColumnsList();
  }
  public void MandatoryOptionalColumnsList()
  {
      List&lt;MandatoryColumns&gt; MColumnsList = new List&lt;MandatoryColumns&gt;();
      MColumnsList.Add(new MandatoryColumns() { MColumnName = "John", MColumnName2 = "Smith" });
      MColumnsList.Add(new MandatoryColumns() { MColumnName = "Jason", MColumnName2 = "Bell" });
      MColumnsListXaml.ItemsSource = MColumnsList;
  }
  </code></pre>
  
  <p>Now I need to get information that is in the ListView and that's where I'm stuck. My intention is to extract a column in a ListView and cross check against another list. To get the values, I've tried <code>foreach</code> loop as per below:</p>
  
  <pre><code>public void test()
  {
      foreach (var item in MColumnsListXaml.Items)
      {
          MessageBox.Show(MColumnsListXaml.ToString()); //MColumnsListXaml = {System.Windows.Controls.ListView Items.Count:2}
          MessageBox.Show(MColumnsListXaml.Items.ToString()); //MColumnsListXaml.Items = {System.Windows.Controls.ItemCollection}
          MessageBox.Show(item.ToString()); //item = {tutorialWpfApplication1.MandatoryColumns}
      }
  }
  </code></pre>
  
  <p>In debugger I see item returns object <code>{tutorialWpfApplication1.MandatoryColumns}</code> and holds my <a href="https://i.imgur.com/BTSh4xu.png" rel="nofollow noreferrer">needed information</a>, but I can't find a way to access it. I tried to iterate <code>item</code> by making it <code>dynamic</code>, declare <code>ListViewItem</code> instead of `var and various solutions online, but everything always resulted in an exception error.</p>