I know this probably simple, but I have been searching google, and I really haven't made much ground.
I would like to take a button for example, and add it to a listbox, programatically, not in the xaml.
My current stategy for doing this is :
Button testButton = new Button();
listbox.Items.add(testButton);
Have you tried this...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Button b = new Button();
b.Content = "myitem";
b.Click += new RoutedEventHandler(b_Click);
listBox1.Items.Add(b);
}
void b_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Item CLicked");
}
ListBox has an Items collection property that you can add any control into it.
var listBox = new ListBox();
var button = new Button()
{
Content = "Click me"
};
var textBlock = new TextBlock()
{
Text = "This is a textblock"
};
listBox.Items.Add(button);
listBox.Items.Add(textBlock);
Add method is expecting an object type so it can take data types like strings, integer, classes that you want to show in the list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With