WPF ComboBox ItemsSource无法正常工作

时间:2014-08-08 10:05:04

标签: c# wpf

我是WPF新手,试图修复现有程序。 有一个ComboBox定义为:

<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}">

尝试在后面的代码中填充它:

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没有任何内容。有什么遗失的吗?

3 个答案:

答案 0 :(得分:3)

在这种情况下,

ItemsSource="{Binding}"是不必要的。

答案 1 :(得分:2)

这可以通过纯绑定来完成。我不太确定您尝试实现的目标,但下面的示例显示了如何挂钩事件并避免代码隐藏。您也可以绑定到SelectedItem。不过这是对你的问题的建议。

的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>

请注意,我在这里使用的是viewmodel,因此您必须将视图的datacontext设置为此类的实例。就个人而言,如果可能的话,我会避免这个事件,而只是绑定到选定项目,并可能创建一个行为,以避免这种耦合。

代码:

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));
    }
 }

希望它有所帮助,

干杯,

了Stian

答案 2 :(得分:0)

  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;
    }

我希望它会对你有所帮助:)。