c# wpf string listbox listboxitem

agregar un ListBoxItem en un ListBox en C#?



listbox wpf binding (3)

Prueba esto:

ListBoxItem itm = new ListBoxItem(); itm.Content = "some text"; listbox.Items.Add(itm);

listbox es el nombre de ListBox.

Yo sé eso:

String test = "test"; ListBox.Items.Add(test);

o

String test = "test"; int index = 1; ListBox.Items.Insert(index, String);

agrega la cadena en un ListBox, pero quiero insertar ListBoxItem, ¿cómo? previamente aprendí eso

var contentToString = (String)ListBoxItem.Content;

simplemente convierte ListBoxItem en String, pero no pude hacer lo contrario para convertir String en ListBoxItem


Puedes hacer eso

ListBox1.Items.Insert(0,new ListItem("ITEM 1", "Value"))


Su objeto siempre estará en un ListBoxItem, el ListBox generará uno para usted si no lo agrega explícitamente. Para obtener el ListBoxItem que usa:

var listboxitem = (ListBoxItem)listbox.ItemContainerGenerator.ContainerFromItem(myItem);