Steve Staple
Steve Staple

Reputation: 3279

WPF ComboBox ItemsSource not working

I am new to WPF, trying to fix up an existing program. There is a ComboBox defined as:

<ComboBox Height="23" Margin="111.5,6.738,6,0" Name="comboBoxDocType" VerticalAlignment="Top" FontFamily="Calibri" FontSize="11" SelectedIndex="0" SelectionChanged="comboBoxDocType_SelectionChanged" ItemsSource="{Binding}">

Trying to populate it in the code behind:

DocumentTypesList = new List<string>();
DocumentTypesList.Add(DocumentTypes.Unknown);  
DocumentTypesList.Add(DocumentTypes.PurchaseOrder);
DocumentTypesList.Add(DocumentTypes.RMInvoice);
DocumentTypesList.Add(DocumentTypes.SundryOne);
DocumentTypesList.Add(DocumentTypes.DevelopmentPaper);
comboBoxDocType.ItemsSource = DocumentTypesList;

ComboBox is coming up with nothing in it. Is there something missing?

Upvotes: 1

Views: 695

Answers (3)

  public MainPage()
    {
        this.InitializeComponent();
        //get menu
         List<listboxitem> menu_list = Load_Menu();
         lst_menu.ItemsSource = menu_list;


    }


    private NavigationHelper NavigationHelper;
    private ObservableDictionary DefaultViewmodel = new ObservableDictionary();
    private string[] Logo_menu_array = { "Assets/star-6-48.ico", "/Assets/note-48.ico", "/Assets/medal-48.ico", "/Assets/joystick-48.ico" };
    private string[] Text_menu_array={"Phổ biến trên YouTuBe","Âm nhạc","Thể thao","Trò chơi"};






    //Menu
    public class listboxitem
    {
        public string textmenu { get; set; }
        public string logomenu { get; set; }
    }
    //load menu
    public List<listboxitem> Load_Menu()
    {
        List<listboxitem> text = new List<listboxitem>();
        for (int i = 0; i < Math.Min(Logo_menu_array.Length, Text_menu_array.Length); i++)
        {
            var l = new listboxitem();
            l.logomenu = Logo_menu_array[i];
            l.textmenu = Text_menu_array[i];
            text.Add(l);
        }

        return text;
    }

I hope it will help you :)

Upvotes: 0

St&#237;gandr
St&#237;gandr

Reputation: 2902

This can be done by pure binding. I'm not quite sure what you try to achieve, but the example below shows how you can hook onto on an event and avoid codebehind. You could just bind to SelectedItem as well. However here is an suggestion for your problem.

Xaml:

<ComboBox Height="23" Margin="111.5,6.738,6,0"  VerticalAlignment="Top" FontFamily="Calibri" FontSize="11" SelectedIndex="0"  ItemsSource="{Binding DocumentTypesList}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <command:EventToCommand Command="{Binding DocumentSelectionChangedCommand}" PassEventArgsToCommand="True"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>

Note that I am using a viewmodel here, so you must set datacontext of your view to an instance of this class. Personally I would have avoided the event if possible, and instead just bind to the selecteditem(s) and maybe create a behaviour, for avoiding this coupling.

Code:

public class YourViewModel : INotifyPropertyChanged

    private ObservableCollection<DocumentTypes> documentTypesList = new ObservableCollection<DocumentTypes> {DocumentTypes.Unknown, DocumentTypes.PurchaseOrder, DocumentTypes.RMInvoice, DocumentTypes.SundryOne, DocumentTypes.DevelopmentPaper};       

    public ObservableCollection<DocumentTypes> DocumentTypesList
    {
       get { return documentTypesList; }
       set
       {
           if (Equals(value, documentTypesList)) return;
           documentTypesList = value;
           OnPropertyChanged();
       }
    }

    public ICommand DocumentSelectionChangedCommand { get; set; }


    public YourViewModel() 
    {
        InitStuff();
    }

    public void InitStuff(){
        DocumentSelectionChangedCommand = new RelayCommand<SelectionChangedEventArgs>(OnDocumentChanged);
    }

    private void OnDocumentChanged(SelectionChangedEventArgs e)
    {
        // To your stuff here, but all this can be done by bindings as well!
        // Invoke in some SelectedDocuments property's setter or something
    }


    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator] // Comment out this line if no R#
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
 }

Hope it helps,

Cheers,

Stian

Upvotes: 2

romanoza
romanoza

Reputation: 4862

ItemsSource="{Binding}" is unnecessary in this context.

Upvotes: 3

Related Questions