title: Proper DataGrid search from TextBox in WPF using MVVM
body: <p>I am new to the MVVM pattern, and a little confused on when to use Code Behind.  I have a very simple form right now, that includes one TextBox, and one DataGrid.  What I would like is to be able to have the DataGrid change its selected item based on the TextBox. </p>
  
  <p>I have done this in Code Behind and it works fine using the following code:</p>
  
  <pre><code>private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
  {
      for (int i = 0; i &lt; dataGrid1.Items.Count; i++)
      {
          string cellContent = dtReferral.Rows[i][0].ToString();
          try
          {
              if (cellContent != null &amp;&amp; cellContent.Substring(0, textBox1.Text.Length).Equals(textBox1.Text))
              {
                  object item = dataGrid1.Items[i];
                  dataGrid1.SelectedItem = item;
                  dataGrid1.ScrollIntoView(item);
                  //row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                  break;
              }
          }
          catch { }
      }
  }
  </code></pre>
  
  <p>Now, I just want to highlight the Item in the Datagrid that starts with text in textbox, and allow the user to press a button to edit selected item.</p>
  
  <p>Is it okay to have this logic in the Code Behind file? Or would I need to do this through some sort of binding?  If I should do this through the View Model with Binding, any direction would be appreciated.  Thank you.</p>