Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6984285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:31:47+00:00 2026-05-27T18:31:47+00:00

So I have a listbox that is being populated by an array and has

  • 0

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:

<ListBox Name="mainListBox" SelectionChanged="mainListBox_SelectionChanged" Width="460" HorizontalAlignment="Center" VerticalAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="5" Orientation="Horizontal" HorizontalAlignment="Left">
                        <Image Source="{Binding data.thumbnail}" Margin="5" VerticalAlignment="Top" Width="70" />
                        <StackPanel Orientation="Vertical">
                            <TextBlock x:Name="TitleInfo" Text="{Binding data.title}" TextWrapping="Wrap" Foreground="DarkSeaGreen" Width="370" />
                                <TextBlock x:Name="AuthorInfo" Text="{Binding data.author}" FontSize="15" Margin="2" />
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Score:" Margin="2" FontSize="14"  />
                                <TextBlock x:Name="score" Text="{Binding data.score}" FontSize="14" Margin="2"/>
                                <HyperlinkButton Content="Comments" Click="HyperlinkButton_Click" FontSize="15" x:Name="commentsLink" />
                            </StackPanel>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

I want this line:

<HyperlinkButton Content="Comments" Click="HyperlinkButton_Click" FontSize="15" x:Name="commentsLink" />

to give me the index in the code behind the xaml file.

How can I go about doing this?

Thanks

edit:
Here’s the code that is being problematic.

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;
    }

Also, here is the code for my object:

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; }
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:31:47+00:00Added an answer on May 27, 2026 at 6:31 pm

    You can use the Tag parameter. Note that the {Binding} binds the entire object itself.

    For example,

    class Myobj
    {
       string param1 { get; set; } 
       string param2 { get; set; }
    }
    
    ObservableCollection<Myobj> collection;
    

    If collection is your ItemsSource, then inside your DataTemplate , a {Binding} refers to an entire instance of Myobj.

    <HyperlinkButton Content="Comments" Click="HyperlinkButton_Click" FontSize="15" x:Name="commentsLink" Tag="{Binding}" />
    

    then in your Click Event, simply cast sender to a hyperlink button, and get the Tag.

    ...
    var hb = sender as HyperLinkButton;
    if (hb != null)
      {
         var obj = hb.Tag as Myobj;
         if (obj != null)
         { 
    
         }
      }
    ...
    

    Check out this cheatsheet on binding. It’s very helpful.

    http://www.nbdtech.com/Free/WpfBinding.pdf

    Note that this implementation does not use SelectedIndex – but it doesn’t need to. Since the HyperLinkButton has a reference to the object that you are generating the ListBoxItem for, there’s no need for it.

    Finally, here’s a sample project

    https://skydrive.live.com/redir.aspx?cid=ef08824b672fb5d8&resid=EF08824B672FB5D8!352&parid=EF08824B672FB5D8!343

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListBox that has a style defined for ListBoxItems. Inside this style,
I currently have a listbox that has its selected item bound to a property
I am new to WPF. I have a ListBox that has its ItemSource set
I have a ListBox that gets populated with items read from a JSON response.
Scenario: I have a list table being populated from a combobox. That list table
I have a ListBox that is being bound to data from a WCF service.
I have a silverlight listbox that is being used as a search result box.
I have a listbox being populated from a SQLDATA pull, and it pulls down
I have a listbox that is populated with a list of List items. The
I have a listbox that is databound to a Collection of objects. The listbox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.