使用MVVM将列表绑定到WPF中的ComboBox [英] Binding List to a ComboBox in WPF using MVVM

查看:182
本文介绍了使用MVVM将列表绑定到WPF中的ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库表中读取列的值,将这些值存储为

I am trying to read the values of a column from a database table, store those values as a

List<string>

并使用MVVM将其绑定到WPF中的组合框。



尽管这些值是从数据库中读取并存储在

and bind it to a combobox in WPF using MVVM.

Although the values are read from the database and stored in a

List<string>

中,但是ComboBox会不要将其显示为下拉菜单。



我已经坚持了2天这个问题。

我们非常感谢任何帮助。



这是我的代码:



ViewModel.cs:

, the ComboBox does not display it as a dropdown.

I have been stuck with this problem for 2 days.
Any Help would be truely appreciated.

Here is my code:

ViewModel.cs:

private List<string> myComboBoxData =null;

        public List<string> MyComboBoxData
        {
           get
           {
               return this.myComboBoxData ;
           }
           set
           {
               this.myComboBoxData = value;
               base.OnPropertyChanged("MyComboBoxData ");
           }
        }

        public ViewModel(UserDetails view)
        {
            this.view = view;
            view.DataContext = this;
            getmyComboBoxData();
        }

        private void getmyComboBoxData()
        {
          using (myEntities context = new myEntities())
            {
                myComboBoxData = (from utype in context.usertypes
                           select utype.TypeName).ToList<string>();
            }
        }





View.xaml:

<ComboBox  ItemsSource="{Binding MyComboBoxData}" Height="23" HorizontalAlignment="Left" Margin="150,91,0,0" Name="mycomboBox" VerticalAlignment="Top" Width="120" />

推荐答案

在p ublic List< string>中MyComboBoxData setter,base.OnPropertyChanged的参数应该是属性的确切名称 - MyComboBoxData - 大写字母M且没有空格
In the public List<string> MyComboBoxData setter, the argument of base.OnPropertyChanged should be exact name of the property - "MyComboBoxData" - capital M and no space.


这篇关于使用MVVM将列表绑定到WPF中的ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆