Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new in the WPF and started learning on it.
I want to make the list of buttons from a list of a class say "Buttons" and having two fields (ButtonContent,ButtonID) in the MainForm in WPF.
The idea behind this is that when the MainForm will be loaded then i want to make buttons list dynamically.
The Best Example is already given in the Link
Animating Interactive 2D Elements in a 3D Panel[^]
But i only want to make the equal size buttons stacked horizontally.
How can i do this in WPF? Thank you in advance.
Posted

Try this
HTML
<listbox.itemspanel>
         <itemspaneltemplate>
             <stackpanel orientation="Horizontal" verticalalignment="Top" />
         </itemspaneltemplate>
     </listbox.itemspanel>
 
Share this answer
 
Comments
WaseemAmin 7-Apr-14 6:59am    
Sir the main functionality which i need is to know how to add the UserContol(Button) as a ListBoxItem in the list box dynamically?
Naz_Firdouse 7-Apr-14 7:05am    
if you have listbox in xaml, then you can add like this
<pre lang="c#">
Button Button1 = new Button();

Button1.Name = "item1";
Button1.Content = "Item1";
Button1.Height = 60;
Button1.Width = 87;
Button1.Margin = new Thickness(10, 5, 0, 5);


listbox1.Items.Add(Button1); //where listbox1 is the name of the listbox
</pre>
WaseemAmin 7-Apr-14 9:38am    
Thank you !
I got the Idea.

Jazak Allah
Naz_Firdouse 8-Apr-14 5:37am    
upvote and Mark as answer if the solution solves your problem.
It will be handy for others also. I am giving my solution here. This will help to make buttons dynamically in a ItemsControl or ListBox.
After some working hard i was able to do it as i wanted.
in my XAML i did like this.


<itemscontrol x:name="ic"><br />
    <itemscontrol.itemtemplate><br />
        <datatemplate><br />
            <Button<br />
                Command="Open"<br />
                CommandParameter="{Binding Path=ID}"<br />
                Cursor="Hand"<br />
                Focusable="False"<br />
                ><br />
                <Button.Template><br />
                    <controltemplate targettype="Button"><br />
                        <border><br />
                            removed="Black"<br />
                            BorderBrush="#88000000"<br />
                            BorderThickness="0.5"<br />
                            Padding="1"<br />
                            ><br />
                            <dockpanel><br />
                                <Button<br />
                                    Background="Transparent"             <br />
                                    Command="{Binding Open}"<br />
                                    CommandParameter="{Binding Path=ID}"<br />
                                    Content="{Binding Path=Content}"<br />
                                    DockPanel.Dock="Top"<br />
                                    Focusable="False"<br />
                                    Foreground="White"<br />
                                    HorizontalAlignment="Stretch"<br />
                                    Margin="0"<br />
                                    ToolTip="Testing Buttons..."<br />
                                        Click="Button_Click"<br />
                                    /><br />
                                <dockpanel.commandbindings><br />
                                    <commandbinding command="Open" executed="Open_Executed" /><br />
                                </dockpanel.commandbindings><br />
                            </dockpanel><br />
                        </border><br />
                    </controltemplate><br />
                </Button.Template><br />
            </Button><br />
        </datatemplate><br />
    </itemscontrol.itemtemplate><br />
</itemscontrol>


then code behind i did like the following:

buttonContentList = MainButtonsList.FindAll(o => (o.MenuLevel == levelID) && (o.GroupID == groupID) && (o.Enable == isEnable));<br />
                this.ic.Items.Clear();<br />
                int buttonIndex = 0;<br />
                foreach (ButtonContent bc in buttonContentList)<br />
                {<br />
                    Button btn = new Button();<br />
                    btn.Click += new RoutedEventHandler(btn_Click);<br />
                    btn.Content = bc.Content;<br />
                    btn.Tag = buttonIndex++;<br />
                    btn.Height = 60;<br />
                    btn.Width = 80;<br />
                    btn.Margin = new Thickness(10, 5, 0, 5);<br />
<br />
                    this.ic.Items.Add(btn);<br />
                }
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900