title: windows store app - gridview selecteditem is null
body: <p>First, I do not speak English well, it is difficult to question</p>
  
  <p>i'm doing this question through the translator</p>
  
  <p>you might not understand what I said.</p>
  
  <p>I have succeeded in creating itemClick event referenced this page.</p>
  
  <p><a href="http://marcominerva.wordpress.com/2013/03/07/how-to-bind-the-itemclick-event-to-a-command-and-pass-the-clicked-item-to-it/" rel="nofollow">http://marcominerva.wordpress.com/2013/03/07/how-to-bind-the-itemclick-event-to-a-command-and-pass-the-clicked-item-to-it/</a></p>
  
  <pre><code>     &lt;GridView x:Name="itemGridView" 
       HorizontalAlignment="Left" Height="434" Margin="14,284,0,0"
       VerticalAlignment="Top" Width="1312" IsItemClickEnabled="True"
       ItemsSource="{Binding ItemList}"  SelectedItem="{Binding DataContext.MyName,Mode=TwoWay}"
       vm:ItemClickCommand.Command="{Binding MyCommand}"&gt;
  </code></pre>
  
  <p>i want set my selected item's value in viewmodel.</p>
  
  <p>i tried set my value in relaycommand. but it did nothing happen.</p>
  
  <pre><code>     public RelayCommand&lt;string&gt; MyCommand
       {
  
          get
          {
              return _myCommand
                  ?? (_myCommand = new RelayCommand&lt;string&gt;(
                                        p =&gt;
                                        {
  
                                            p = "test"; 
                                            MyName = "test"; // nothing happen.
                                        }));
          }
      }
  </code></pre>
  
  <p>how can i make this happen?</p>
  
  <p>add my source code.</p>
  
  <p>xaml.cs</p>
  
  <pre><code>    public sealed partial class MainPage : Page
      {
          public MainPage()
        {
          this.InitializeComponent();
          (DataContext as MainViewModel).MainView = this;
          //itemGridView.SelectedItem = (DataContext as MainViewModel).MyName;
        }
  
  
      }
  </code></pre>
  
  <p>viewmodel.cs</p>
  
  <pre><code>    public class MainViewModel : ViewModelBase
  {
      /// &lt;summary&gt;
      /// Initializes a new instance of the MainViewModel class.
      /// &lt;/summary&gt;
      public MainViewModel()
      {
          ////if (IsInDesignMode)
          ////{
          ////    // Code runs in Blend --&gt; create design time data.
          ////}
          ////else
          ////{
          ////    // Code runs "for real"
          ////}
         // MusicList = new ObservableCollection&lt;Mp3Model&gt;();
          ItemList = new ObservableCollection&lt;string&gt;();
  
      }
      public MainPage MainView { get; set; }
  
      #region MyName
  
      private string _MyName;
      /// &lt;summary&gt;
      /// TODO:Change Comment
      /// &lt;/summary&gt;
      public string MyName
      {
          get
          {
              return _MyName;
          }
          set
          {
  
              Set&lt;string&gt;(ref _MyName, value);
          }
      }
  
      #endregion MyName
  
  
  
      private ObservableCollection&lt;string&gt; _itemList;
      public ObservableCollection&lt;string&gt; ItemList
      {
          get { return _itemList; }
          set
          {
              Set&lt;ObservableCollection&lt;string&gt;&gt;(ref _itemList, value);
          }
      }
  
  
      private RelayCommand&lt;string&gt; _myCommand;
  
      /// &lt;summary&gt;
      /// Gets the MyCommand.
      /// &lt;/summary&gt;
      public RelayCommand&lt;string&gt; MyCommand
      {
          get
          {
              return _myCommand
                  ?? (_myCommand = new RelayCommand&lt;string&gt;(
                                        p =&gt;
                                        {
  
                                            p = "test";
                                            MyName = "test"; // nothing happen.
                                        }));
          }
      }
  
      public static class ItemClickCommand
  {
  
      public static readonly DependencyProperty CommandProperty =
      DependencyProperty.RegisterAttached("Command", typeof(ICommand),
      typeof(ItemClickCommand), new PropertyMetadata(null, OnCommandPropertyChanged));
  
      public static void SetCommand(DependencyObject d, ICommand value)
      {
          d.SetValue(CommandProperty, value);
      }
  
      public static ICommand GetCommand(DependencyObject d)
      {
          return (ICommand)d.GetValue(CommandProperty);
      }
  
      private static void OnCommandPropertyChanged(DependencyObject d,
          DependencyPropertyChangedEventArgs e)
      {
          var control = d as ListViewBase;
  
          if (control != null)
              control.ItemClick += OnItemClick;
      }
  
      private static void OnItemClick(object sender, ItemClickEventArgs e)
      {
          var control = sender as ListViewBase;
  
          var command = GetCommand(control);
          System.Diagnostics.Debug.WriteLine(control.SelectedItem); // null
          if (command != null &amp;&amp; command.CanExecute(e.ClickedItem))
              command.Execute(e.ClickedItem);
      } 
  }
  </code></pre>
  
  <p>xaml</p>
  
  <pre><code>   &lt;Page
  DataContext="{Binding Main, Source={StaticResource Locator}}"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="using:VisionTest"
  xmlns:vm="using:VisionTest.ViewModel"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:CustomControl="using:VisionTest.CustomControl"
  xmlns:Converters="using:VisionTest.Converter"
  xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
  xmlns:triggers="using:WinRT.Triggers"
  x:Class="VisionTest.MainPage"
  
  
  mc:Ignorable="d"&gt;
  &lt;Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"&gt;
  
      &lt;Button Margin="1087,107,0,623" Content="OpenFileTest" Command="{Binding FileOpenCommand, Mode=OneWay}"&gt;&lt;/Button&gt;
      &lt;MediaElement Source="{Binding MyMediaPlayer}"&gt;&lt;/MediaElement&gt;
      &lt;GridView 
          x:Name="itemGridView" HorizontalAlignment="Left" Height="434" Margin="14,284,0,0" VerticalAlignment="Top" Width="1312" IsItemClickEnabled="True"
                 ItemsSource="{Binding ItemList}"  SelectedItem="{Binding DataContext.MyName,Mode=TwoWay}"
                 vm:ItemClickCommand.Command="{Binding MyCommand}"&gt;
      &lt;/GridView&gt;
  
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>