21

そんなこと知ってる:

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

また

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

ListBox に文字列を追加しますが、ListBoxItem を挿入したいのですが、どうすればよいですか? 以前に私はそれを学びます

var contentToString = (String)ListBoxItem.Content;

ListBoxItem を String に変換するだけですが、逆に String を ListBoxItem に変換することはできませんでした

4

3 に答える 3

37

これを試して:

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

listbox.Items.Add(itm);

listbox は ListBox の名前です。

于 2012-11-07T10:35:18.570 に答える
1

オブジェクトは常に ListBoxItem にあり、明示的に追加しない場合は ListBox が生成します。使用する ListBoxItem を取得するには:

var listboxitem = (ListBoxItem)listbox.ItemContainerGenerator.ContainerFromItem(myItem);
于 2012-11-07T10:43:50.077 に答える
1

あなたはそのようにすることができます

ListBox1.Items.Insert(0,new ListItem("ITEM 1", "Value"))
于 2012-11-07T10:29:55.857 に答える