title: Set SelectedItem in GridView in Windows Store Application
body: <p>I have a model object which a list of it binds to a Gridview. this is my model class</p>
  
  <pre><code>public class ItemModel: BindableBase
  {
  
  
      public override bool Equals(object obj)
      {
          return this.Id == ((ItemModel)obj).Id;
      }
  
      public override int GetHashCode()
      {
          return Id.GetHashCode();
      }
      public static bool operator ==(ItemModel op1, ItemModel op2)
      {
          return  op1.Id == op2.Id;
      }
  
      public static bool operator !=(ItemModel op1, ItemModel op2)
      {
          return op1.Id != op2.Id;
      }
  
  
  
      private Guid _id;
      public Guid Id
      {
          set { SetProperty(ref this._id, value); }
          get { return _id; }
      }
  
      private string _clue;
      public string Clue
      {
          set { this.SetProperty(ref this._clue, value); }
          get { return _clue; }
      }
  
      private string _boxId;
      public string BoxId
      {
          set { this.SetProperty(ref this._boxId, value); }
          get { return _boxId; }
      }
  
      public override string ToString()
      {
          return this.Clue;
      }
  </code></pre>
  
  <p>there are two ways for loading the data in my page. first with an integer number and second with and ItemModel object. for second way the grid must be selected the corresponding item which passed to the page. so my code is shown in following:</p>
  
  <pre><code>protected override async void LoadState(Object navigationParameter, Dictionary&lt;String, Object&gt; pageState)
      {
          // TODO: Assign a bindable collection of items to this.DefaultViewModel["Items"]
  
          if (navigationParameter is int)
          {
              BindByBoxId(navigationParameter);
          }
          else if (navigationParameter is ItemModel)
          {
              BindByWordObject(navigationParameter as ItemModel);
          }
  
      }
  
      private async void BindByWordObject(ItemRepository passedItem)
      {
          ItemRepository repo = new ItemRepository();
          var boxId = (int)passedItem.BoxId;
          var item = await repo.GetWordsByBox(boxId);
          this.DefaultViewModel["Items"] = item;
          GroupNameTextBlock.Text = passedItem.Box.BoxName;
  
  
          itemGridView.SelectedIndex = GetItemIndex(passedItem);
          itemGridView.SelectionChanged += SelectChanged;
  
      }
  
      private int GetItemIndex(ItemModel item)
      {
          for(int i =0 ; i&lt;itemGridView.Items.Count; i++)
          {
              if (((ItemModel)itemGridView.Items[i]) == item)
                  return i;
          }
          return -1;
      }
  
  
  
      private async void BindByBoxId(object navigationParameter)
      {
          ItemRepository repo = new ItemRepository();
          var boxId = (int)navigationParameter;
          var item = await repo.GetItemsByBox(boxId);
          this.DefaultViewModel["Items"] = item;
          GroupNameTextBlock.Text = item.First().Box.BoxName;
          itemGridView.SelectedValue = -1;
          itemListView.SelectedValue = -1;
  
          itemGridView.SelectionChanged += SelectChanged;
      }
  </code></pre>
  
  <p>I try <code>SelectedValue</code> and <code>SelectedItem</code> (in windows store application, they can be used as seter and geter ) but these properties never work well. My solution for selected the corresponding item in the gridview is looping through the gridview's items and find the item index ( <code>GetItemIndex</code> method ) and using <code>SelectedIndex</code> property. I want to know is there any alternative and optimized way for me? because looping in large number of items would be awful!!! please advice me.</p>