title: UWP Databinding: How to set button command to parent DataContext inside DataTemplate
body: <p>Short explanation of need: I need to fire the command of a button inside a DataTemplate, using a method from the DataContext of the ViewModel.</p>
  
  <p>Short explanation of problem: The templated button command only seems to be bindable to the datacontext of the item itself. The syntax used by WPF and Windows 8.1 apps to walk up the visual tree doesn't seem to work, including ElementName and Ancestor binding.  I would very much prefer not to have my button command located inside the MODEL.</p>
  
  <p>Side Note: This is built with the MVVM design method.</p>
  
  <p>The below code generates the list of items on the VIEW. That list is one button for each list item.</p>
  
  <pre><code>            &lt;ItemsControl x:Name="listView" Tag="listOfStories" Grid.Row="0" Grid.Column="1"
              ItemsSource="{x:Bind ViewModel.ListOfStories}"
              ItemTemplate="{StaticResource storyTemplate}"
              Background="Transparent"
              IsRightTapEnabled="False"
              IsHoldingEnabled="False"
              IsDoubleTapEnabled="False"
                   /&gt;
  </code></pre>
  
  <p>Inside the page resources of the same VIEW, I have created a DataTemplate, containing the problematic button in question.  I went ahead and stripped out most of the formatting inside the button, such as text, to make the code easier to read on this side.  Everything concerning the button works, except for the problem listed, which is the binding of the command.</p>
  
  <pre><code>&lt;Page.Resources&gt;
          &lt;DataTemplate x:Name="storyTemplate" x:DataType="m:Story"&gt;
              &lt;Button
                  Margin="0,6,0,0"
                  Width="{Binding ColumnDefinitions[1].ActualWidth, ElementName=storyGrid, Mode=OneWay}"
                  HorizontalContentAlignment="Stretch"
                  CommandParameter="{Binding DataContext, ElementName=Page}"
                  Command="{Binding Source={StaticResource Locator}}"&gt;
  
                  &lt;StackPanel HorizontalAlignment="Stretch" &gt;
                      &lt;TextBlock Text="{x:Bind StoryTitle, Mode=OneWay}"
                          FontSize="30"
                          TextTrimming="WordEllipsis"
                          TextAlignment="Left"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/Button&gt;
          &lt;/DataTemplate&gt;
      &lt;/Page.Resources&gt;
  </code></pre>
  
  <p>Because this is a DataTemplate, the DataContext has been set to the individual items that comprise the list (MODEL).  What I need to do is select the DataContext of the list itself (VIEWMODEL), so I can then access a navigation command.</p>
  
  <p>If you are interested in the code-behind of the VIEW page, please see below.</p>
  
  <pre><code>    public sealed partial class ChooseStoryToPlay_View : Page
      {
      public ChooseStoryToPlay_View()
      {
          this.InitializeComponent();
          this.DataContextChanged += (s, e) =&gt; { ViewModel = DataContext as ChooseStoryToPlay_ViewModel; };
      }
      public ChooseStoryToPlay_ViewModel ViewModel { get; set; }
  }
  </code></pre>
  
  <p>I've tried setting it by ElementName, among many other attempts, but all have failed.  Intellisense detects "storyTemplate" as an option when ElementName is input, which is the name of the DataTemplate shown in the first code block of this question.</p>
  
  <p>I don't believe my problem can be unique, however I'm having great difficulty finding a solution for UWP. Allow me to apologize in advance in this is a simple question, but I've spent nearly two days researching answers, with none seeming to work for UWP.</p>
  
  <p>Thank you guys!</p>