Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有WCF服务列表的数据绑定组合框_C#_Wpf_Wcf_Mvvm_Combobox - Fatal编程技术网

C# 具有WCF服务列表的数据绑定组合框

C# 具有WCF服务列表的数据绑定组合框,c#,wpf,wcf,mvvm,combobox,C#,Wpf,Wcf,Mvvm,Combobox,我正在为windows应用商店开发一个应用程序(MVVM模式),使用WCF服务从数据库接收数据。 我想将一个类别列表数据绑定到combobox中,但它对我不起作用,我搜索了web,仍然没有找到解决方案 类别: public Category(Category c) { this.Id=c.Id; this.Name = c.Name; } public int Id { get; set; } public string N

我正在为windows应用商店开发一个应用程序(MVVM模式),使用WCF服务从数据库接收数据。 我想将一个类别列表数据绑定到combobox中,但它对我不起作用,我搜索了web,仍然没有找到解决方案

类别:

   public Category(Category c)
    {
        this.Id=c.Id;
        this.Name = c.Name;

    }
    public int Id { get; set; }
    public string Name { get; set; }
Xaml:


视图模型:

public ObservableCollection<Category> ListCategories { get; private set; }
public observeCollection ListCategories{get;private set;}
在OnNavigatedTo函数中:

   var listCategory = await proxy.GetAllCategoriesAsync();
            List<Category> list = new List<Category>();
            foreach (var item in listCategory)
            {

                list.Add(new Category(item));
            }
            ListCategories = new ObservableCollection<Category>(list);
var listCategory=await proxy.GetAllCategoriesAsync();
列表=新列表();
foreach(listCategory中的var项)
{
列表。添加(新类别(项目));
}
ListCategories=新的ObservableCollection(列表);

有人吗?

您需要实现
INotifyPropertyChanged
,以便让UI知道您已更改了
列表类别
集合

在ViewModel中,实现interface
INotifyPropertyChanged

public class YourViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }

    private ObservableCollection<Category> _categories;
    public ObservableCollection<Category> ListCategories
    {
        get { return _categories; }
        set
        {
            if (_categories != value)
            {
                _categories = value;
                OnPropertyChanged("ListCategories");
            }
        }
    }
public类YourViewModel:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged(字符串propertyName)
{
var handler=PropertyChanged;
if(处理程序!=null)
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
私有可观测集合_类;
公共可观察收集列表类别
{
获取{return\u categories;}
设置
{
如果(_categories!=值)
{
_类别=价值;
OnPropertyChanged(“列表类别”);
}
}
}

您是否调试了实际失败的部分?您是否在OnNavigatedTo函数中获得了值?输出部分是否有错误?您的ViewModel是否实现了INotifyPropertyChanged?请提供更多信息。
public class YourViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }

    private ObservableCollection<Category> _categories;
    public ObservableCollection<Category> ListCategories
    {
        get { return _categories; }
        set
        {
            if (_categories != value)
            {
                _categories = value;
                OnPropertyChanged("ListCategories");
            }
        }
    }