title: Get data from selected row in Gridview in C#, WPF
body: <p>I am trying to retrieve data from a Gridview that I have created in XAML.</p>
  
  <pre><code>&lt;ListView Name="chartListView" selectionChanged="chartListView_SelectionChanged"&gt;
    &lt;ListView.View&gt;
       &lt;GridView&gt;
          &lt;GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="250"/&gt;
          &lt;GridViewColumn Header="Type" DisplayMemberBinding="{Binding Type}" Width="60"/&gt;
          &lt;GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}" Width="100"/&gt;
       &lt;/GridView&gt;
    &lt;/ListView.View&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>I have seen some code like this :-</p>
  
  <pre><code>GridViewRow row = GridView1.SelectedRow;
  TextBox2.Text = row.Cells[2].Text;
  </code></pre>
  
  <p>However my problem is that my GridView is created in XAML, and is not named, ie I cannot (or do not know how to) create a reference to 'gridview1', and therefore cannot access objects within it.</p>
  
  <p>Can I name or create a reference to my gridview either from c# or XAML so I can use the above code?</p>
  
  <p>Secondly, can I then access the array elements by name instead of index, something like :-</p>
  
  <pre><code>TextBox2.Text = row.Cells["ID"].Text
  </code></pre>
  
  <p>Thanks for any help.</p>