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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T01:52:44+00:00 2026-05-21T01:52:44+00:00

I wanted to know how can I populate a ListBox dinamically. But I don’t

  • 0

I wanted to know how can I populate a ListBox dinamically. But I don’t want a custom class, I just want a normal selector.

I normally do something like this with my own classes:

<ListBox Name="itemList" SelectionChanged="itemList_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <TextBlock Text="{Binding title}"/>
                <TextBlock Text="{Binding subtitle}"  />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

What I don’t know is how to simply generate the simplest thing: something like this:

<ListBox>
    <ListBoxItem Name="item1" Content="First item" />
    <ListBoxItem Name="item2" Content="Second item" />
    <ListBoxItem Name="item3" Content="Third item" />
</ListBox>

Could you give an example of the structure? I don’t know if I have to use datatemplate or what…

Thank you

[Edited: added Name property in what I need]

  • 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-21T01:52:45+00:00Added an answer on May 21, 2026 at 1:52 am

    You just have to set up a Collection with your items in your DataContext and set it as ListBox.ItemsSource. This will then fill in your DataTemplate.

    See for example:

    <Grid>
        <Grid.Resources>
            <src:Customers x:Key="customers"/>
        </Grid.Resources>
        <ListBox ItemsSource="{StaticResource customers}" Width="250" Margin="0,5,0,10" 
           DisplayMemberPath="LastName"/>
    </Grid>
    

    And:

    public class Customer
    {
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public String Address { get; set; }
    
        public Customer(String firstName, String lastName, String address)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Address = address;
        }
    
    }
    
    public class Customers : ObservableCollection<Customer>
    {
        public Customers()
        {
            Add(new Customer("Michael", "Anderberg",
                    "12 North Third Street, Apartment 45"));
            Add(new Customer("Chris", "Ashton",
                    "34 West Fifth Street, Apartment 67"));
            Add(new Customer("Cassie", "Hicks",
                    "56 East Seventh Street, Apartment 89"));
            Add(new Customer("Guido", "Pica",
                    "78 South Ninth Street, Apartment 10"));
        }
    
    }
    

    Example source: http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemssource(v=vs.95).aspx

    Edit: according to the discussion in the comments, maybe you only need a simple ItemsControl (as you don’t need to retain the selected item or even better to handle multiple selection, which is what the ListBox is for).

    For example:

    <ItemsControl ItemsSource="{Binding NavigateItems}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Content="{Binding Label}"
                        Command="{Binding ButtonCommand}"                                                               
                        CommandParameter="{Binding URL}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    And:

    public class NavigateItem
     {
         public String Label { get; set; }
         public String URL { get; set; }
    
         public NavigateItem(String label, String url)
         {
             this.Label = label;
             this.URL = url;
         }
    
     }
    
    public class NavigateItems : ObservableCollection<NavigateItem>
    {
        public NavigateItems()
        {
            Add(new NavigateItem("Google", "http://www.google.com");
            Add(new NavigateItem("Bing", "http://www.bing.com");
        }
    }
    

    And of course setting up the ButtonCommand to navigate to the URL passed in the parameter, but that depends on how you’re setting the ViewModel / bindings up.

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

Sidebar

Related Questions

I just wanted to know if I can point to class using this implementation:
I just started to work with Silverlight and wanted to know how I can
I know I can do that using switch(), but still I wanted to know
Just wanted to know can we create web service with Axis2 without using eclipse
1.i just wanted to know can i start the the service without creating activity
I wanted to know how can I get request to XML file (.xml) but
Just wanted to know can i directly run the code which EJB code (adhering
I wanted to know how can we get the index of the array if
Thanks for looking at this question. I wanted to know how can I use
Hello I am new to MATLAB , I wanted to know how can I

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.