c# windows 8 selected comboboxitem
本文关键字:comboboxitem selected windows | 更新日期: 2023-09-27 18:10:51
我正在开发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 selectionchanged时,我这样做:
private void cboTeam1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Ploeg1.Text = cboTeam1.SelectedValue.ToString();
}
结果是:SportsBetting.Model.TeamItem
谁能帮助我得到组合框的选择值在我的文本块(Ploeg1.Text)??
你几乎自己回答了这个问题。
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;
}
另一个选择是重写TeamItem类中的ToString()来返回Description。在这种情况下,原始代码应该可以正常工作。
public override string ToString()
{
return this._description; // assumes you have a backing store of this name
}