c#windows 8选定的comboboxitem
我正在开发一个Windows8商店应用程序(c#)。 我有一个组合框(cboTeam1),可以从存储库中获取项目c#windows 8选定的comboboxitem,c#,windows-8,combobox,C#,Windows 8,Combobox,我正在开发一个Windows8商店应用程序(c#)。 我有一个组合框(cboTeam1),可以从存储库中获取项目 private static List<TeamItem> JPLItems = new List<TeamItem>(); public static List<TeamItem> getJPLItems() { if (JPLItems.Count == 0) { JPLItem
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选择更改时,我会执行以下操作:
private void cboTeam1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Ploeg1.Text = cboTeam1.SelectedValue.ToString();
}
这将导致:sportsbotting.Model.TeamItem
有人能帮我在我的文本块(Ploeg1.Text)中获取组合框selectedvalue吗?你几乎自己回答了这个问题
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()以返回描述。在这种情况下,您的原始代码应该可以正常工作
public override string ToString()
{
return this._description; // assumes you have a backing store of this name
}
哇,为什么我没看到这个:)非常感谢!!如何检查cboTeam1中的所选项目与cboTeam2中的所选项目是否不同?最好作为新问题提问:
public override string ToString()
{
return this._description; // assumes you have a backing store of this name
}