开发者

How to bind images to a Combobox in WPF, getting the images from a lookup

I have a WPF combobox that I would like to bind to an observable collection of Teams on my ViewModel class e.g.

class Team 
{
  public int Id { get; set; }
  public string Name { get; set; }
  public int CountryId { get; set; }
}

class ViewModel
{
   public ObservableCollection<Team> Teams { get; set; }
   public IDictionary<int, Image> CountryFlags { get; set; }
}

I'd like to have the combobox display the fl开发者_运维百科ag and name of each team, what is the best way to do this?

I don't want to add the image directly to the team object if I can avoid it and would be happy to move the CountryFlags lookup to another class if need be.


You'll need to make a custom IValueConverter for this. Basically, you'll want to have the converter convert from Team -> Image, and have it do the lookup within the Dictionary based on the CountryId in the Team.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜