0

Windows 8 ストア アプリケーション (c#) を開発しています。リポジトリからアイテムを取得するコンボボックス (cboTeam1) があります。

private static List<TeamItem> JPLItems = new List<TeamItem>();

public static List<TeamItem> getJPLItems()
    {
        if (JPLItems.Count == 0)
        {
            JPLItems.Add(new TeamItem() { Id = 1, Description = "Anderlecht", Image = "Jpl/Anderlecht.png", ItemType = ItemType.JPL });
            JPLItems.Add(new TeamItem() { Id = 1, Description = "Beerschot", Image = "Jpl/Beerschot.png", ItemType = ItemType.JPL });
            JPLItems.Add(new TeamItem() { Id = 1, Description = "Cercle Brugge", Image = "Jpl/Cercle.png", ItemType = ItemType.JPL });
            JPLItems.Add(new TeamItem() { Id = 1, Description = "Charleroi", Image = "Jpl/Charleroi.png", ItemType = ItemType.JPL });
        }
        return JPLItems;
    }

cboTeam1 の ItemsSource にアイテムをロードします。

cboTeam1.ItemsSource = ItemRepository.getJPLItems();

cboTeam1 の選択が変更されたら、次のようにします。

    private void cboTeam1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Ploeg1.Text = cboTeam1.SelectedValue.ToString();               
    }

この結果: SportsBetting.Model.TeamItem

私のテキストブロック(Ploeg1.Text)でコンボボックスのselectedvalueを取得するのを手伝ってくれる人はいますか??

4

1 に答える 1

1

あなたはほとんど自分でこれに答えました。

private void cboTeam1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
        // cast the selected item to the correct type.
    var selected = cboTeam.SelectedValue as TeamItem;
        //then access the appropriate property on the object, in this case "Description"
        // note that checking for null would be a good idea, too.
    Ploeg1.Text = selected.Description;               
}

もう 1 つのオプションは、TeamItem クラスで ToString() をオーバーライドして Description を返すことです。その場合、元のコードは正常に動作するはずです。

public override string ToString()
{
    return this._description;  // assumes you have a backing store of this name
}
于 2013-04-24T08:24:23.350 に答える