Listbox 所选项目内容到文本块
我确信有一个简单的解决方案,但目前我似乎找不到。
我正在尝试使用以下代码将文本块中选择列表框的内容显示为文本。
private void SelectionToText(object sender, EventArgs e)
{
ListBoxItem selection = (ListBoxItem)TextListBox.SelectedItem;
selectionText.Text = "This is the " + selection;
}
由于某种原因,文本块只显示
“这是 System.Windows.Controls.ListBoxItem ”
我最初认为这是因为我没有转换为字符串,但这也不起作用。
有什么建议吗?
I am sure there is a simple solution to this, but I can't seem to find it at the moment.
I am trying to disaply the content of the selection listbox in a textblock as text using the below code.
private void SelectionToText(object sender, EventArgs e)
{
ListBoxItem selection = (ListBoxItem)TextListBox.SelectedItem;
selectionText.Text = "This is the " + selection;
}
For some reason the textblock just displays
"This is the System.Windows.Controls.ListBoxItem "
I initial thought it was because I hasn't converted to a string, but that didn't work either.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以引用 ListBoxItem 的 Content 属性
You can reference the Content property of the ListBoxItem
您可以创建一个自定义类
将项目添加到您的
ListBox
中,例如:这将起作用
You can create a custom class
Add items to your
ListBox
like:And this will work
如果我没记错的话,你需要执行以下代码
这将返回 SelectedItem 的值
If I am not wrong, you need to do the following code
This will return the value of SelectedItem
请这样写:
Please write as this:
或者,您可以在 silverlight 中通过将文本块的 text 属性绑定到列表框的 selecteditem.content 属性来实现无需后台代码。
其中 list 是我的列表框的名称。
Or you can do it without code behind, in silverlight, by binding the text property of the textblock to the selecteditem.content property of the listbox.
Where list is the name of my ListBox.