阅读该产品的价值在ListBox [英] Read the Value of an Item in a listbox

查看:131
本文介绍了阅读该产品的价值在ListBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发C#中的小窗户商店应用程序在那里我有填充的内容作为一个的列表框的使用以下$ C $ç片段。

I'm developing a small windows store application in C# where I have populated the values and content for a listbox using the following code snippet.

code 1:添加歌曲标题为项目的到的列表框的,使用乐曲的的创造的项目

code 1 : adds the song title as a item to a listbox, using the Song class to create the item

    private void addTitles(string title, int value) 
    {
        Song songItem = new Song(); 
        songItem.Text = title;
        songItem.Value = value;
        listbox1.Items.Add(songItem); // adds the 'songItem' as an Item to listbox
    }

code 2:歌的的用来设置数值为每个项目的('songItem')

code 2 : Song class which is used to set values to each item ('songItem')

public class Song
{
    public string Text { get; set; }
    public int Value { get; set; }
    public override string ToString()
    {
        return Text;
    }
}

人口的内容到的列表框的目前运行正常。

我要的是让值每项目的上一个Click事件,在运行时。

What I want is to get the 'Value' of each item on a Click event, on run-time.

为此,我怎么能读取的列表框的所选项目(摘录)在后,在C#中? (该值是songItem.Value)

For that purpose, how can I read(extract) the Value of the selected Item in the listbox, in C#? ( Value is the songItem.Value )

code 3:我已经试过这code,因为试图找出一个解决方案,但没有奏效。

code 3 : I have tried this code, as trying to figure out a solution, but it didn't work

 private void listbox1_tapped(object sender, TappedRoutedEventArgs e)
        {
           Int selectedItemValue = listbox1.SelectedItem.Value();
        }

因此​​这将是非常感激,如果有人能帮助我,因为我是一个业余爱好者。

Therefore it would be really grateful if someone can help me, as I'm an Amateur.

推荐答案

不知道的TappedRoutedEventArgs,但我会做

not sure about the "TappedRoutedEventArgs", but I would do

private void listbox1_tapped(object sender, TappedRoutedEventArgs e)
        {
           var selectedSong = (Song)listbox1.SelectedItem;
           if (selectedSong != null) {
              var val = selectedSong.Value;
              }
        }

由于的SelectedItem 对象(不知道关于属性),所以你必须将它转换为歌曲第一。

because SelectedItem is an Object (which doesn't know about the Value property), so you have to cast it to a Song first.

顺便说一句,属性,而不是,所以你不需要括号。

By the way, Value is a property, not a method, so you don't need the parenthesis.

这篇关于阅读该产品的价值在ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆