Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting the index from a hyperlink inside of a listbox
    text
    copied!<p>So I have a listbox that is being populated by an array and has 25 entries in it. In each row the listbox has, I have a "comments" hyperlink button that has separate functionality than what the listbox does. So, since I'm not technically selecting the listbox item, it doesn't return an index. Anyways, here's the code:</p> <pre><code>&lt;ListBox Name="mainListBox" SelectionChanged="mainListBox_SelectionChanged" Width="460" HorizontalAlignment="Center" VerticalAlignment="Stretch"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Margin="5" Orientation="Horizontal" HorizontalAlignment="Left"&gt; &lt;Image Source="{Binding data.thumbnail}" Margin="5" VerticalAlignment="Top" Width="70" /&gt; &lt;StackPanel Orientation="Vertical"&gt; &lt;TextBlock x:Name="TitleInfo" Text="{Binding data.title}" TextWrapping="Wrap" Foreground="DarkSeaGreen" Width="370" /&gt; &lt;TextBlock x:Name="AuthorInfo" Text="{Binding data.author}" FontSize="15" Margin="2" /&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="Score:" Margin="2" FontSize="14" /&gt; &lt;TextBlock x:Name="score" Text="{Binding data.score}" FontSize="14" Margin="2"/&gt; &lt;HyperlinkButton Content="Comments" Click="HyperlinkButton_Click" FontSize="15" x:Name="commentsLink" /&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; </code></pre> <p>I want this line:</p> <pre><code>&lt;HyperlinkButton Content="Comments" Click="HyperlinkButton_Click" FontSize="15" x:Name="commentsLink" /&gt; </code></pre> <p>to give me the index in the code behind the xaml file.</p> <p>How can I go about doing this?</p> <p>Thanks</p> <p>edit: Here's the code that is being problematic.</p> <pre><code>private void HyperlinkButton_Click(object sender, RoutedEventArgs e) { var hb = sender as HyperlinkButton; if (hb != null) { var obj = hb.Tag as RootObject; if (obj != null) { MessageBox.Show(obj.data.children[0].data.title, obj.data.children[0].data.author, MessageBoxButton.OK); } } textBlock1.Text = Global.sUrl; } </code></pre> <p>Also, here is the code for my object:</p> <pre><code>public class MediaEmbed { public string content { get; set; } public int? width { get; set; } public bool? scrolling { get; set; } public int? height { get; set; } } public class Oembed { public string provider_url { get; set; } public string description { get; set; } public string title { get; set; } public string url { get; set; } public string author_name { get; set; } public int height { get; set; } public int width { get; set; } public string html { get; set; } public int thumbnail_width { get; set; } public string version { get; set; } public string provider_name { get; set; } public string thumbnail_url { get; set; } public string type { get; set; } public int thumbnail_height { get; set; } public string author_url { get; set; } } public class Media { public string type { get; set; } public Oembed oembed { get; set; } } public class Data2 { public string domain { get; set; } public MediaEmbed media_embed { get; set; } public object levenshtein { get; set; } public string subreddit { get; set; } public string selftext_html { get; set; } public string selftext { get; set; } public object likes { get; set; } public bool saved { get; set; } public string id { get; set; } public bool clicked { get; set; } public string title { get; set; } public Media media { get; set; } public int score { get; set; } public bool over_18 { get; set; } public bool hidden { get; set; } public string thumbnail { get; set; } public string subreddit_id { get; set; } public string author_flair_css_class { get; set; } public int downs { get; set; } public bool is_self { get; set; } public string permalink { get; set; } public string name { get; set; } public double created { get; set; } public string url { get; set; } public string author_flair_text { get; set; } public string author { get; set; } public double created_utc { get; set; } public int num_comments { get; set; } public int ups { get; set; } } public class Child { public string kind { get; set; } public Data2 data { get; set; } } public class Data { public string modhash { get; set; } public Child[] children { get; set; } public string after { get; set; } public object before { get; set; } } public class RootObject { public string kind { get; set; } public Data data { get; set; } } </code></pre> <p>RootObject contains data, which leads to child(an array), which leads to data2 which has all the information I want. Thanks a lot for your help up to this point.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload