title: Passing the value of an item selected in a Gridview to the ViewModel of a different Usercontrol
body: <p>After researching a solution to this for three days, finally I gave up. Now I need your help to solve this.</p>
  
  <h2>Scenario:</h2>
  
  <p>I've a GridView in a Usercontrol (Lets say WLMSLogs.xaml) and My GridView ItemSource is binded to a List from the ViewModel (WMLSLogsViewModel.cs)</p>
  
  <p>Lets say the List has 4 items (EventID, Name, Request and Response). BothRequest and Responses are XML Strings.</p>
  
  <p>GridView needs to display some of the List items in RowDetailsTemplate under different tab items. So I'm displaying Request and Response under respective TabItems.</p>
  
  <pre><code>&lt;GridView x:Name="WMLSLogGrid" ItemsSource="{Binding WMLSLogModelList}"&gt;
  &lt;GridView.Columns&gt;      
      &lt;GridViewDataColumn DataMemberBinding="{Binding ID}" Header="ID"/&gt;
      &lt;GridViewDataColumn DataMemberBinding="{Binding UserName}" Header="UserName"/&gt;     
  &lt;/GridView.Columns&gt;
  &lt;GridView.RowDetailsTemplate&gt;
      &lt;DataTemplate &gt;                                                 
              &lt;TabControl&gt;
                  &lt;TabItem Header="Request Xml"&gt;
                      &lt;TextBlock text="{Binding Request}"/&gt;
                  &lt;/TabItem&gt;
              &lt;TabItem Header="Response Xml"&gt;
                  &lt;TextBlock text="{Binding Response}"/&gt;
              &lt;/TabItem&gt;
                  &lt;TabItem Header="EventLogs" Tag="{Binding .}"&gt;
                      &lt;views:LogEvents  /&gt;
                  &lt;/TabItem&gt;
              &lt;/TabControl&gt;
          &lt;/Grid&gt;
      &lt;/DataTemplate&gt;
  &lt;/GridView.RowDetailsTemplate&gt;
  </code></pre>
  
  <p></p>
  
  <p>WMLSLogModelList is a Observable collection in WMLSLogsViewModel.cs   </p>
  
  <p>So far everything works fine and Grid is displaying data as expected. </p>
  
  <p>Now When User expands any row, he can see two TabItems with request and response.
  Here I need to add one more TabItem (LogEvents) besides Request and Response tabs.</p>
  
  <p>This LogEvents tab is going have one more GridView to display (so I added a new View <code>&lt;views:LogEvents /&gt;</code> in the tab). Here comes the tricky part. </p>
  
  <p>This LogEvents GridView needs to get the data based on the corresponding <code>Selecteditem</code> (which is <code>EventId</code>), and pass this <code>EventId</code> to a different ViewModel (LogEventViewModel.cs) and binds the data to the Inner GridView dynamically. All this has to happen either as I expand the RowDetails section or if I select the Tab LogEvents.</p>
  
  <p>There is no relation between the data items of these two Grids, except getting the Selected EventId of the main GridView and passing this to a different ViewModel then to Domain service to get the inner GridView Data.</p>
  
  <h2>What I did so far</h2>
  
  <p>As I mentioned I created a new View UserControl for LogEvents, Placed it under new TabItem(EventLogs) inside row details template of main GridView.</p>
  
  <p>LogEvent UserControl contains a Grid View binded to LogEventsViewModel to get the Collection based on Selected row EventId.</p>
  
  <p>How do I assign the Selected EventId to a new ViewModel and Get the data dynamically?</p>
  
  <p>One Way: As I showed you, I called LogEvents by placing it in side TabItem. whenever I expanded any row, Then It is Initializing the LogEvents page, During that I tried to bind the Datacontext to LogEventsViewModel. But I'm unable to get the Seleted row EventId dynamically. If I get that then I can easily pass it to the LogEventsViewModel constructor.</p>
  
  <pre><code>       var _viewModel = new LogEventsViewModel(EventId);
         this.DataContext = _viewModel;            
         InitializeComponent();
  </code></pre>
  
  <p>Other way: 
  Pass the selected EventId directly from xaml View binding to that page initialization and then to LogEventsViewModel 
  Something like this </p>
  
  <pre><code>&lt;views:LogEvents  something="{Binding EventId}"/&gt;
  </code></pre>
  
  <p>Is there any other way to do this?</p>
  
  <h2>Details:</h2>
  
  <p>LogEvents.xaml </p>
  
  <pre><code>&lt;UserControl.Resources&gt;
      &lt;viewModel:LogEventsViewModel x:Key="ViewModel"/&gt;
  &lt;/UserControl.Resources&gt;
  &lt;Grid DataContext="{Binding Source={StaticResource ViewModel}}"&gt;
      &lt;GridView x:Name="LogEventsGrid" ItemsSource="{Binding View}"            
                   &lt;GridView.Columns&gt;
              &lt;telerik:GridViewToggleRowDetailsColumn /&gt;              
              &lt;telerik:GridViewDataColumn DataMemberBinding="{Binding EventId}" Header="LogEventId"/&gt;  
              &lt;telerik:GridViewDataColumn DataMemberBinding="{Binding Exception}" Header="Exception" /&gt;
          &lt;/GridView.Columns&gt;
      &lt;/telerik:RadGridView&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>LogEvents.xaml.cs</p>
  
  <pre><code>public int EventId
      {
          get { return (int)GetValue(EventIdProperty); }
          set { SetValue(EventIdProperty, value); }
      }
      public static readonly DependencyProperty EventIdProperty =
      DependencyProperty.Register("EventId", typeof(int), typeof(ApplicationLog),
      new PropertyMetadata(0, new PropertyChangedCallback(OnEventIdChanged)));
  
      private static void OnEventIdChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {            
          int LogEventId1 = (int)e.NewValue;
          // Need to assign propery in LogEventsViewModel 
      }
  </code></pre>
  
  <p>LogEventsViewModel.cs</p>
  
  <pre><code>WMLCDomainContext _Context = new WMLCDomainContext();
      private QueryableDomainServiceCollectionView&lt;LogEvents&gt; view;
      public int _eventid;       
  
      public ApplicationLogsViewModel()
      {
          EntityQuery&lt;LogEvents&gt; getLogEventsQuery = _Context.GetApplicationLogListQuery(EventId);
          this.view = new QueryableDomainServiceCollectionView&lt;ApplicationLog&gt;(_Context, getLogEventsQuery );                        
      }
  
      public IEnumerable View
      {get {return this.view;}}
  
      public int EventId
      {
          get{return this._eventid;}
          set{_eventid = value;}
      }
  </code></pre>