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 8153529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:02:51+00:00 2026-06-06T16:02:51+00:00

I understand that if one has a listBox one can easily add items to

  • 0

I understand that if one has a listBox one can easily add items to it doing something like this:

ListBox myList = new ListBox();
myList.Items.Add("Added Item 1");
myList.Items.Add("Added Item 2");
myList.Items.Add("Added Item 3");

I initially do not know how many items will be added to the list so I don’t add static content like I do here, I iterate through a for loop and add items that way. Moving on…

How can I not just add items to the listbox, but also make each one clickable. For Example say have an array of 12 GUIDs, and for each guid Item I add to the listBox .i.e.

myList.Items.Add(guidArray[i]);

And then react differently depending on what guid was selected?

And another thing, how does one style listBox Items? Other than them just showing up as boring strings stacked under each other?

Thanks in advance!

  • 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-06-06T16:02:53+00:00Added an answer on June 6, 2026 at 4:02 pm

    Suppose you have a collection of GUIDs.

    var guids = new List<Guid>();
    for (var i = 0; i < 12; i++)
    {
        guids.Add(Guid.NewGuid());
    }
    

    You can then bind this collection to your ListBox control.

    myList.ItemsSource = guids;
    

    The ListBox control will automatically update depending on the contents of your guids collection.

    To handle the click event for an item in the ListBox, you need to handle the control’s SelectionChanged event.

    Hook up the event handler.

    <ListBox Name="MyList" 
             SelectionChanged="MyList_SelectionChanged">
    ...
    </ListBox>
    

    Now you can write the necessary code in the event handler:

    private void MyList_SelectionChanged(object sender, 
        System.Windows.Controls.SelectionChangedEventArgs e)
    {
        // Find out which item was clicked
        if (e.AddedItems.Count > 0)
        {
            var myGuid = e.AddedItems[0] as Guid;
            if (myGuid == null)
                return;
            // ...
        }
    }
    

    To style each item in the ListBox. Well that’s a whole other question. Start with binding your ListBox to your datasource (GUID collection in this case).

    Then in XAML you can freely design the contents of the ListBox. Use the ItemTemplate and DateTemplate of the ListBox. In the template you can specify what the ListBox should render for each item.

    Let’s render a TextBlock in a 1×1 grid for each item. Just bind the TextBlock’s Text property to one of the ListBox item’s properties.

    <ListBox Name="MyList" 
         SelectionChanged="MyList_SelectionChanged">
        <ListBox.ItemTemplate>
           <DataTemplate>               
               <Grid>
                   <Grid.ColumnDefinitions>
                       <ColumnDefinition Width="*" />
                       <ColumnDefinition Width="*" />
                   </Grid.ColumnDefinitions>
    
                   <Grid.RowDefinitions>
                       <RowDefinition Height="*" />
                   </Grid.RowDefinitions>
    
                   <TextBlock Grid.Column="1" Text="{Binding ...}" />
               </Grid> 
           </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From web pages like this one, http://www.jsftutorials.net/components/step5.html I understand that the binding attribute in
Something that has always bothered me is doing more than one loop to manipulate
Why only one overload throws this exception? Little update: I understand that there was
I am new to using numpy and one thing that I really don't understand
Suppose that one has some lock based code like the following where mutexes are
I understand that one can use pip to install packages from svn etc. ala:
I understand that if your program has large segments that can be executed in
I searched for this, but couldn't find a solution. I understand that one could
I understand that the cyphertext from a properly used one time pad cypher reveals
One of the most difficult things about understand Spring is that Spring supports multiple

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.