用两个不同的对象填充组合框

时间:2015-01-29 14:58:14

标签: c# wpf entity-framework interface combobox

我想使用界面使用两个不同的对象填充组合框。这就是我目前得到的。这有效但现在我想为每个对象都有一个显示成员和值成员,我该怎么做?

在Controller.cs中

public List<IMusic> Populate()
        {
            List<IMusic> newList = new List<IMusic>();

            foreach(Track t in tr.GetAllTracks()){
                newList.Add(t);
            }

            foreach (Artist a in ar.GetAllArtists())
            {
                newList.Add(a);
            }
            return newList;
        }

IMusic.cs

  interface IMusic
    {

    }

带有DataSource的组合框:

cBMainScreen_Search.DataSource = controller.Populate();

GetAllTracks():

public List<Track> GetAllTracks()
        {
            return db.Track.ToList();
        }

GetAllArtists():

 public List<Artist> GetAllArtists()
        {
            return db.Artist.ToList();
        }

1 个答案:

答案 0 :(得分:0)

只需在界面中设置一些属性:

interface IMusic
{
    string Display { get; set; }
    string Value { get; set; }
}

然后在你的Track类中(应该实现IMusic):

public string Display
{
    get 
    {
        return this.TrackName;
    }
    set 
    {
        this.TrackName= value;
    }
}

public string Value
{
    get 
    {
        return this.TrackID;
    }
    set 
    {
        this.TrackID= value;
    }
}

在您的Artist类中(也实现了IMusic):

public string Display
{
    get 
    {
        return this.ArtistName;
    }
    set 
    {
        this.ArtistName= value;
    }
}

public string Value
{
    get 
    {
        return this.AritstID;
    }
    set 
    {
        this.AritstID= value;
    }
}