Listbox 所选项目内容到文本块

发布于 2024-12-11 12:26:30 字数 432 浏览 0 评论 0原文

我确信有一个简单的解决方案,但目前我似乎找不到。

我正在尝试使用以下代码将文本块中选择列表框的内容显示为文本。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦忆晨望 2024-12-18 12:26:30

您可以引用 ListBoxItem 的 Content 属性

selectionText.Text= "This is the " + selection.Content.ToString();

You can reference the Content property of the ListBoxItem

selectionText.Text= "This is the " + selection.Content.ToString();
野稚 2024-12-18 12:26:30
string selText = selection.Items[selection.SelectedIndex].Text;
string selText = selection.Items[selection.SelectedIndex].Text;
陌伤浅笑 2024-12-18 12:26:30

您可以创建一个自定义类

public class MyListBoxItem
{
    public MyListBoxItem(string value, string text)
    {
        Value = value;
        Text = text;
    }

    public string Value { get; set; }
    public string Text { get; set; }

    public override string ToString()
    {
        return Text;
    }
}

将项目添加到您的 ListBox 中,例如:

listBox1.Items.Add(new MyListBoxItem("1", "Text"));

这将起作用

private void SelectionToText(object sender, EventArgs e)
{
    MyListBoxItem selection = (MyListBoxItem)TextListBox.SelectedItem;

    selectionText.Text = "This is the " + selection;

}

You can create a custom class

public class MyListBoxItem
{
    public MyListBoxItem(string value, string text)
    {
        Value = value;
        Text = text;
    }

    public string Value { get; set; }
    public string Text { get; set; }

    public override string ToString()
    {
        return Text;
    }
}

Add items to your ListBox like:

listBox1.Items.Add(new MyListBoxItem("1", "Text"));

And this will work

private void SelectionToText(object sender, EventArgs e)
{
    MyListBoxItem selection = (MyListBoxItem)TextListBox.SelectedItem;

    selectionText.Text = "This is the " + selection;

}
别挽留 2024-12-18 12:26:30

如果我没记错的话,你需要执行以下代码

Convert.ToString(TextListBox.SelectedItem);

这将返回 SelectedItem 的值

If I am not wrong, you need to do the following code

Convert.ToString(TextListBox.SelectedItem);

This will return the value of SelectedItem

你的呼吸 2024-12-18 12:26:30

请这样写:

private void SelectionToText(object sender, EventArgs e)
{
    MyListBoxItem selection = (MyListBoxItem)TextListBox.SelectedItem;

    selectionText.Text = "This is the " + selection.Content.ToString();

}

Please write as this:

private void SelectionToText(object sender, EventArgs e)
{
    MyListBoxItem selection = (MyListBoxItem)TextListBox.SelectedItem;

    selectionText.Text = "This is the " + selection.Content.ToString();

}
独留℉清风醉 2024-12-18 12:26:30

或者,您可以在 silverlight 中通过将文本块的 text 属性绑定到列表框的 selecteditem.content 属性来实现无需后台代码。

<TextBlock Text="{Binding SelectedItem.Content, ElementName=list}"/>

其中 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.

<TextBlock Text="{Binding SelectedItem.Content, ElementName=list}"/>

Where list is the name of my ListBox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文