Binding data to ComboBox WPF

前端 未结 3 1136
情歌与酒
情歌与酒 2021-01-25 13:51

I am newbie to WPF, and needs help to bind data into the ComboBox. The xaml file contains the tag below.



        
3条回答
  •  生来不讨喜
    2021-01-25 14:21

    I assume your Inbox class consists of two properties (for simplicity), but there may be any number of them:

    public class Inbox
    {
        public int ID { get; set; }
        public string Text { get; set; }
    }
    

    You write a DataTemplate, for example:

    
        
            
                
                :
                
            
        
    
    

    Then correct your ComboBox declaration like:

    
    

    Finally you set DataContext of your ComboBox to your List:

    public void FillInboxes(List inboxes)
    {
       _currentInbox.DataContext = inboxes;
    }
    

    EDIT: As you've asked for a simpler solution, you can just override ToString() method of your Inbox class:

    protected override string ToString()
    {
        return ID.ToString() + ":" + Text;
    }
    

提交回复
热议问题