开发者

Get selected item value from listbox

开发者 https://www.devze.com 2023-03-05 16:38 出处:网络
public void select(int z) { ListBox1.SelectedIndex = z; ListBox1.SelectedItem = ListBox1.Items[z]; ListBoxItem
public void select(int z)
{

    ListBox1.SelectedIndex = z;

    ListBox1.SelectedItem = ListBox1.Items[z];                

    ListBoxItem 
    selectedItem=ListBox1.ItemContainerGenerator.ContainerFromItem(this.ListBox1.SelectedItem)    
    as ListBoxItem;

    selectedItem.Focus();

}

In this method I'm trying to select element in the listbox, But the reference variable carrying value is null which is returning by the selec开发者_Python百科tion code.

"ListBox1.ItemContainerGenerator.ContainerFromItem(this.ListBox1.SelectedItem) as ListBoxItem;"

I have debugged my code and keenly observed that the values are coming into the ListBox1 object, but still it is returning a null value.


ListBoxItem selectedItem = ListBox1.ItemContainerGenerator.ContainerFromItem(this.ListBox1.SelectedItem) as ListBoxItem;
TextBox1.Text = selectedItem.Content.ToString();

or is it TextBox1.Context = selectedItem.Content.ToString();

Good luck :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号