程序是这样写的:
ListBoxItem newItem = new ListBoxItem();
newItem.Content = news[i];
newItem.Tag = newItems[i];
this.listBox1.Items.Add(newItem); 显示:
MessageBox.Show(listBox1.Items[listBox1.SelectedIndex].ToString());
ListBoxItem newItem = new ListBoxItem();
newItem.Content = news[i];
newItem.Tag = newItems[i];
this.listBox1.Items.Add(newItem); 显示:
MessageBox.Show(listBox1.Items[listBox1.SelectedIndex].ToString());
{
public string Content
{
get;
set;
}
public string Tag { get; set; } public override string ToString()
{
return this.Tag;
}
}
上面是看你的代码猜的..只要在ListBoxItem类里面重写 ToString()方法就行
或者:
MessageBox.Show(((ListBoxItem)(listBox1.SelectedItem)).Content);
public override string ToString()
{
return this.Tag;//要显示Content的话就是 return this.Content;
}
ok?
//绑定数据
List<string> st = new List<string>();
st.Add("conteng1");
st.Add("conteng2");
listBox1.ItemsSource = st;//触发事件
string a = listBox1.Items[listBox1.SelectedIndex].ToString();
MessageBox.Show(a); 这样就可以显示你想要的结果了