title: Getting the Tag value of a Control
body: <p>Based from this <code>XAML</code>,</p>
  
  <pre><code>&lt;ListView&gt;
      &lt;ListView.View&gt;
          &lt;GridView&gt;
              &lt;GridViewColumn&gt;
                  &lt;GridViewColumn.CellTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;StackPanel Orientation="Horizontal"
                                      VerticalAlignment="Center"
                                      HorizontalAlignment="Center"&gt;
                              &lt;TextBlock Text="{Binding ProductDescription}"/&gt;
                           &lt;/StackPanel&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/GridViewColumn.CellTemplate&gt;
              &lt;/GridViewColumn&gt;
              &lt;GridViewColumn&gt;
                  &lt;GridViewColumn.CellTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;StackPanel Orientation="Horizontal"
                                      VerticalAlignment="Center"
                                      HorizontalAlignment="Center"&gt;
                              &lt;TextBox x:Name"txtExchange"
                                       Tag="{Binding ProductBarcode}"/&gt;
                              &lt;Button Content="Add"
                                      Tag="{Binding ProductBarcode}"
                                      Click="SelectExchangeProduct" /&gt;
                           &lt;/StackPanel&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/GridViewColumn.CellTemplate&gt;
              &lt;/GridViewColumn&gt;
          &lt;/GridView&gt;
      &lt;/ListView.View&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>since all rows inserted will have the same <code>TextBox</code>, using <code>x:Name</code> to get the value is impossible.</p>
  
  <p>I added binding to the <code>Tag</code> property so that I can differentiate all the <code>TextBox</code>. The problem is how can I find that specific <code>TextBox</code>? I tried using <a href="https://stackoverflow.com/questions/12238599/find-wpf-control-by-name"><code>FindName()</code></a>, but I don't know how to get that <code>TextBox</code> with a specific <code>Tag</code> value.</p>
  
  <p>I also tried this:</p>
  
  <pre><code>var txtExchangeQuantity = someParentControl.Children.OfType&lt;TextBox&gt;().Where(x =&gt; x.Tag.ToString() == barcode).FirstOrDefault();
  </code></pre>
  
  <p>but also didn't work. I saw potential to that <code>Where</code> part, but don't know how to implement it.</p>
  
  <p>Here is the <code>Click</code> event:</p>
  
  <pre><code>private void SelectExchangeProduct(object sender, RoutedEventArgs e)
      {
          Button btn = sender as Button;
          string barcode = btn.Tag.ToString();
          var txtExchangeQuantity = grdExchange.Children.OfType&lt;TextBox&gt;().Where(x =&gt; x.Tag.ToString() == barcode).FirstOrDefault();
  
      }
  </code></pre>
  
  <p>Here is the object that I'm binding to <em>(using <code>ObservableCollection</code>)</em>:</p>
  
  <pre><code>class TransactionList{
      private string _productBarcode;
      public string ProductBarcode
      {
          get { return _productBarcode; }
          set { _productBarcode = value; }
      }
      private string _productDescription;
      public string ProductDescription
      {
          get { return _productDescription; }
          set { _productDescription = value; }
      }
  }
  </code></pre>