Alex Biddle

Alex Biddle

  • NA
  • 10
  • 5.3k

Windows 8.1 blank app data bind to next page

Aug 16 2016 2:50 PM
I am creating a Windows 8.1 blank application in C#. The purpose of the application is to be a periodic table app which will be able to give users information about the chemical elements. However I have come into an issue where I haven't yet found a way to 'bind' data to a second page via a button click.<div></div><div>Below is the code I have been using to set the values on the MainPage.xaml.cs page to populate the TextBlocks on the 'ElementPage.xaml' depending on the button user clicks</div><div></div><div> <div class="dp-highlighter"><div class="bar"></div><ol class="dp-c"><li class="alt"><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;Elements&nbsp;&nbsp;</span></span></li><li><span>&nbsp;{&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">string</span><span>&nbsp;PeriodicElementName&nbsp;{&nbsp;</span><span class="keyword">get</span><span>;&nbsp;</span><span class="keyword">set</span><span>;&nbsp;}&nbsp;&nbsp;</span></span></li><li><span>&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">string</span><span>&nbsp;ElementAtomicNumber&nbsp;{&nbsp;</span><span class="keyword">get</span><span>;&nbsp;</span><span class="keyword">set</span><span>;&nbsp;}&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">string</span><span>&nbsp;ElementAtomicMass&nbsp;{&nbsp;</span><span class="keyword">get</span><span>;&nbsp;</span><span class="keyword">set</span><span>;&nbsp;}&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">string</span><span>&nbsp;ElementDescription&nbsp;{&nbsp;</span><span class="keyword">get</span><span>;&nbsp;</span><span class="keyword">set</span><span>;&nbsp;}&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;Elements&nbsp;ShowElements(</span><span class="keyword">string</span><span>&nbsp;Name,&nbsp;</span><span class="keyword">string</span><span>&nbsp;AtomNumber,&nbsp;</span><span class="keyword">string</span><span>&nbsp;AtomMass,&nbsp;</span><span class="keyword">string</span><span>&nbsp;ElementDesc)&nbsp;&nbsp;</span></span></li><li><span>&nbsp;{&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;var&nbsp;ElementsInfo&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Elements()&nbsp;&nbsp;</span></span></li><li><span>&nbsp;{&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;PeriodicElementName&nbsp;=&nbsp;<span class="string">"Name:&nbsp;"</span><span>&nbsp;+&nbsp;Name,&nbsp;&nbsp;</span></span></li><li><span>&nbsp;ElementAtomicMass&nbsp;=&nbsp;<span class="string">"Atomic&nbsp;Mass:&nbsp;"</span><span>&nbsp;+&nbsp;AtomMass,&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;ElementAtomicNumber&nbsp;=&nbsp;<span class="string">"Atomic&nbsp;Number:&nbsp;"</span><span>&nbsp;+&nbsp;AtomNumber,&nbsp;&nbsp;</span></span></li><li><span>&nbsp;ElementDescription&nbsp;=&nbsp;<span class="string">"Description:&nbsp;"</span><span>&nbsp;+&nbsp;ElementDesc&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;};&nbsp;&nbsp;</span></li><li><span>&nbsp;<span class="keyword">return</span><span>&nbsp;ElementsInfo;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;}&nbsp;&nbsp;</span></li></ol></div></div><div></div><div>I.e. whenever the user clicks on the Lithium button they will be taken to a page which will then show information based off of the data I have set in the class in the behind code file.</div><div></div><div> </div><div></div><div> <br />I followed the answer described in the question http://stackoverflow.com/questions/24946575/passing-data-from-page-to-page-for-windows-phone-8-1</div><div></div><div>However, it did not work for me.</div><div></div><div>I have tried one method which did work which involved creating a public string variable named 'ElementChoice' in my 'MainPage.xaml.cs' file and then changing the variable value to the name of the element whenever that specific element's button was clicked.</div><div></div><div> <div class="dp-highlighter"><div class="bar"></div><ol class="dp-c"><li class="alt"><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;</span><span class="keyword">string</span><span>&nbsp;ElementChoice;&nbsp;&nbsp;</span></span></li></ol></div></div><div></div><div><br /> <div class="dp-highlighter"><div class="bar"></div><ol class="dp-c"><li class="alt"><span><span class="keyword">private</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;btnLith_Click(</span><span class="keyword">object</span><span>&nbsp;sender,&nbsp;RoutedEventArgs&nbsp;e)&nbsp;&nbsp;</span></span></li><li><span>&nbsp;{&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;ElementChoice&nbsp;=&nbsp;<span class="string">"Lithium"</span><span>;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;}&nbsp;&nbsp;</span></li></ol></div></div><div></div><div> </div><div></div><div>I would then have an if statement on the page I am navigating to named 'ElementPage.xaml.cs' saying </div><div></div><div> </div><div class="dp-highlighter"><div class="bar"></div><ol class="dp-c"><li class="alt"><span><span class="keyword">if</span><span>&nbsp;(ElementChoice==</span><span class="string">"Lithium"</span><span>)&nbsp;&nbsp;</span></span></li><li><span>&nbsp;{&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;DataContext&nbsp;=&nbsp;Elements.ShowElements(<span class="string">"Lithium"</span><span>,&nbsp;</span><span class="string">"3"</span><span>,&nbsp;</span><span class="string">"6.94"</span><span>,&nbsp;</span><span class="string">"This&nbsp;is&nbsp;Lithium"</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;}&nbsp;&nbsp;</span></li></ol></div><div></div><div><br />However, I don't want to really have to create if statements for every single button.</div><div></div><div>So I was wondering if anybody could give me any guidance on how to pass the data from one page to another which could then bind the data depending on what button was clicked on the application?</div>