MVVM属性未在View中刷新

时间:2016-10-26 13:41:25

标签: c# wpf mvvm

我有以下View Model类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using DOSTreeListModule.DataAdapter;
using WebApi2.SpaceDBData;
using Microsoft.Practices.Prism.Mvvm;
using Meijer.Merch.Shared.Infrastructure; 


namespace DOSTreeListModule.ViewModel
{
    public class DOSAdminProductHrcyViewModel : INotifyPropertyChanged
    {
        private readonly IWebApiDataAdapter _webAPIDataAdapter;

        public DOSAdminProductHrcyViewModel()
    {
        this._webAPIDataAdapter = new DosAdminDataAdapter();
        //this.ProductList = new Task<List<Product>>(this.GetProductList());
        //OnPropertyChanged("ProductList");
    }

    public List<Product> prdLst;

    public List<Product> ProductList
    {
        get
        {
            GetProductList();
            return prdLst;
        }
        set
        {
            ProductList = value;
        }
    }



    private async void GetProductList()
    {
        try
        {
            List<Product> dbProductList = new List<Product>();
            var result = await _webAPIDataAdapter.GetProductHierarchy();
            foreach (Product prd in result)
            {
                dbProductList.Add(prd);
            }
            this.prdLst = dbProductList;
            //return dbProductList;
            OnPropertyChanged("ProductList");

        }
        catch (Exception ex)
        {
            throw (ex);
        }
    }
        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

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

        #endregion
    }
}

以下是使用ProductList属性的WPF视图中的TreeListControl,该属性已在上面填充:

<dxg:TreeListControl Name ="treeList" AutoGenerateColumns="AddNew" 
                             ItemsSource="{Binding ElementName=root, Path=ProductList.Result,Mode=TwoWay}"
                                 EnableSmartColumnsGeneration="True" RenderTransformOrigin="0.5,0.5" 
                                 Grid.ColumnSpan="2" Margin="0,0,0,0" Height="300">            
            <dxg:TreeListControl.Columns>
                <dxg:TreeListColumn FieldName="P_Hierarchy" Header="P_Hierarchy" />
                <dxg:TreeListColumn FieldName="COMBasis" Header="Compliance Basis" />
                <dxg:TreeListColumn FieldName="COMStandard" Header="Compliance Standard"/>
                <dxg:TreeListColumn FieldName="DOSBase" Header="DOS Standard Base" />
                <dxg:TreeListColumn FieldName="DOSStandard" Header="DOS Standard Promo" />
            </dxg:TreeListControl.Columns>
            <dxg:TreeListControl.View>
                <dxg:TreeListView Name="treeListView1" 
                                  TreeDerivationMode="Selfreference" KeyFieldName="ID" ParentFieldName="KeyID"
                                  ShowExpandButtons="False" AllowCascadeUpdate="True"
                                  ShowTotalSummary="True"/>
            </dxg:TreeListControl.View>
        </dxg:TreeListControl>

以下是我的View代码:

namespace DOSTreeListModule.View
{
    /// <summary>
    /// Interaction logic for DosAdminProductHierarchy.xaml
    /// </summary>
    public partial class DosAdminProductHierarchy : UserControl
    {
        public DosAdminProductHierarchy(DOSAdminProductHrcyViewModel vm)
        {
            InitializeComponent();            
            this.DataContext = vm;
        }
    }
}

问题是我的TreeList控件没有被填充。虽然我在GetProductList()方法中获取数据(大约7000行),但我没有在前端看到任何数据。请帮忙。

3 个答案:

答案 0 :(得分:0)

看起来你绑定了错误的属性。您的Xcode 8.1应该绑定到视图模型中的ItemsSource

ProductList

应该是:

ItemsSource="{Binding ElementName=root, Path=ProductList.Result,Mode=TwoWay}"

ItemsSource="{Binding Path=ProductList}" 是树中显示的项目。

答案 1 :(得分:0)

首先将视图模型更新为更灵活的

public class DOSAdminProductHrcyViewModel : INotifyPropertyChanged {

    private readonly IWebApiDataAdapter _webAPIDataAdapter;

    public DOSAdminProductHrcyViewModel() {
        this._webAPIDataAdapter = new DosAdminDataAdapter();
        ProductList = new ObservableCollection<Product>();
    }

    public ObservableCollection<Product> ProductList {get; set;}

    public async Task GetProductListAsync() {
        try {
            var result = await _webAPIDataAdapter.GetProductHierarchy();
            ProductList = new ObservableCollection<Product>(result);
            OnPropertyChanged("ProductList");    
        } catch (Exception ex) {
            throw (ex);
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

现在在视图中(代码隐藏)

public partial class DosAdminProductHierarchy : UserControl {
    DOSAdminProductHrcyViewMode viewModel;

    public DosAdminProductHierarchy(DOSAdminProductHrcyViewModel vm) {
        InitializeComponent();            
        this.viewModel = vm;
        this.DataContext = this.viewModel;
        this.Loaded += async delegate {
            await this.viewModel.GetProductListAsync();
        }      
    }
}

最后在视图中(XAML)仅绑定到集合

ItemsSource="{Binding Path=ProductList}"

答案 2 :(得分:0)

根据我的信息,我会尝试:

public List<Product> ProductList
    {
        get
        {
            GetProductList();
            return prdLst;
        }
        set
        {
            prdLst = value;
            OnPropertyChanged("ProductList");
        }
    }

 private async void GetProductList()
    {
        try
        {
            List<Product> dbProductList = new List<Product>();
            var result = await _webAPIDataAdapter.GetProductHierarchy();
            foreach (Product prd in result)
            {
                dbProductList.Add(prd);
            }
            if(this.prdLst != dbProductList)
                this.ProductList = dbProductList;
        }
        catch (Exception ex)
        {
            throw (ex);
        }
    }



ItemsSource="{Binding ElementName=YourWindowsName.DataContext, Path=ProductList,Mode=OneWay}"