LIstbox Selected Item content to textblock

不羁岁月 提交于 2019-11-29 11:59:43

You can reference the Content property of the ListBoxItem

selectionText.Text= "This is the " + selection.Content.ToString();
string selText = selection.Items[selection.SelectedIndex].Text;

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;

}

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

Convert.ToString(TextListBox.SelectedItem);

This will return the value of SelectedItem

Please write as this:

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

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

}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!