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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:04:19+00:00 2026-06-19T00:04:19+00:00

I want to create a listbox that contains buttons, and each time one button

  • 0

I want to create a listbox that contains buttons, and each time one button is selected (animation) my code is like this:

<UserControl x:Class="sa.UserControl2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="108" d:DesignWidth="592" xmlns:my="clr-namespace:sa">

    <Grid Width="572">
        <ListBox  Name="ListBoxV"  ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                  Height="96" HorizontalAlignment="Left" VerticalAlignment="Top"
                  Width="572"
                  ItemsSource="{Binding ListItems}" >
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="Padding" Value="30 0 30 0" />
                </Style>
            </ListBox.ItemContainerStyle>


        </ListBox>
    </Grid>
</UserControl>


 public UserControl2()
    {
        InitializeComponent();

           List<Temp> ListItems = new List<Temp>();
            for (int i = 0; i < 20; i++)
            {
                ListItems.Add(new  Temp("a"+i));
            }

            ListBoxV.ItemsSource = ListItems;

            DispatcherTimer dt = new DispatcherTimer();

            dt.Tick += new EventHandler(dt_Tick);

            dt.Interval = TimeSpan.FromSeconds(2);

            dt.Start();
        }

        void dt_Tick(object sender, EventArgs e)
        {
            if ((ListBoxV.SelectedIndex + 1) < ListBoxV.Items.Count)
                ListBoxV.SelectedIndex = ListBoxV.SelectedIndex + 1;
            else
                ListBoxV.SelectedIndex = 0;
            ListBoxV.ScrollIntoView(ListBoxV.SelectedItem);
        }

        public class Temp
        {
               public Temp(string s)
               {Button b = new Button();
                b.Name=s;            }


        }
    }

The listbox don’t display buttons, only the animation is working
affich “sa.UserControl2.Temp” for every element.

When displaying if the end of the list, it want back to the begin of the list.

  • 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-19T00:04:21+00:00Added an answer on June 19, 2026 at 12:04 am

    You have several issues here. I suggest you read up on DataTemplates and MVVM

    First of all, the items you use to fill in the ListBox (or whatever other ItemsControl-based element) should be Data Items, stricly. These cannot contain such thing as Buttons. It’s much better to maintain Application Logic and UI separated in WPF.

    Here is an example of your ListBox with an ItemTemplate:

    <ListBox  Name="ListBoxV"  ScrollViewer.HorizontalScrollBarVisibility="Hidden"
              Height="96" HorizontalAlignment="Left" VerticalAlignment="Top"
              Width="572"
              ItemsSource="{Binding ListItems}" >
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Padding" Value="30 0 30 0" />
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
           <DataTemplate>
               <Button Content="{Binding Property}"/>
           </DataTemplate>
    </ListBox>
    

    Data Item:

    public class Temp
    {
        string Property {get;set;}
    
        public Temp(string s)
        {
            Property = s;
        }
    }
    

    Also note that in order to support updates on the UI when you change these properties in the class Temp, it will have to implement the System.ComponentModel.INotifyPropertyChanged interface.

    Then, if you want to perform an action when the button is clicked for each item, you will have to use an ICommand:

    public class Temp
    {
        string Property {get;set;}
    
        public Temp(string s)
        {
            Property = s;
            DoSomethingCommand = new DelegateCommand(x => DoSomething());
        }
    
        public DelegateCommand DoSomethingCommand {get;set;}
    
        private void DoSomething()
        {
            //DoSomething when the Button is Clicked!!
        }
    }
    

    XAML:

    <Button Content="{Binding Property}" Command="{Binding DoSomethingCommand}"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a custom control type that behaves exactly like a ListBox
I want to create a Style for a WPF ListBox that includes a Button
I want to create a ListBox that looks like the Windows Live Tiles .
I want to create a listbox like this: -----|-----|-----|-----|-----|----- The |'s are my listboxitems
i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
I want to create a listbox that'll be bound to XPath, relative to the
Hi I want to create a looping listbox so that the last item's next
I want to create an app that has a listbox on the left side
I want to merge between my code that contains the Web service and the
I want to create a ListBox control that allows the user to edit items,

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.