WPF DataContext不使用MVVM模型刷新DataGrid

时间:2009-05-25 17:46:24

标签: wpf mvvm datacontext

项目概述

我有一个绑定到包含2个ObserverableCollection的viewmodel的视图。 viewmodel构造函数填充第一个ObserverableCollection,并收集视图datacontext以通过名为Sites的公共属性绑定到它。

稍后,在LoadOrders方法中填充2ed ObserverableCollection,并更新公共属性LoadFraudResults以将其与datacontext绑定。

我正在使用WCF从数据库中提取数据并将其拉得非常好。

VIEWMODEL来源

class ManageFraudOrderViewModel:ViewModelBase 
{
    #region Fields        
    private readonly ICollectionView collectionViewSites;
private readonly ICollectionView collectionView;
    private ObservableCollection<GeneralAdminService.Website> _sites;
    private ObservableCollection<FraudService.OrderQueue> _LoadFraudResults;

    #endregion 

    #region Properties
    public ObservableCollection<GeneralAdminService.Website> Sites 
    {
        get { return this._sites; }
    }
    public ObservableCollection<FraudService.OrderQueue> LoadFraudResults 
    {
        get { return this._LoadFraudResults;}
    }
    #endregion

    public ManageFraudOrderViewModel()
    {
        //Get values from wfc service model
        GeneralAdminService.GeneralAdminServiceClient generalAdminServiceClient = new GeneralAdminServiceClient();
        GeneralAdminService.Website[] websites = generalAdminServiceClient.GetWebsites();
        //Get values from wfc service model            

        if (websites.Length > 0)
        {
            _sites = new ObservableCollection<Wqn.Administration.UI.GeneralAdminService.Website>();
            foreach (GeneralAdminService.Website website in websites)
            {
                _sites.Add((Wqn.Administration.UI.GeneralAdminService.Website)website);
            }

    this.collectionViewSites= CollectionViewSource.GetDefaultView(this._sites);
        }
        generalAdminServiceClient.Close();
    }

    public void LoadOrders(Wqn.Administration.UI.FraudService.Website website)
    {
        //Get values from wfc service model            
        FraudServiceClient fraudServiceClient = new FraudServiceClient();
        FraudService.OrderQueue[] OrderQueue = fraudServiceClient.GetFraudOrders(website);
        //Get values from wfc service model            

        if (OrderQueue.Length > 0)
        {
            _LoadFraudResults = new ObservableCollection<Wqn.Administration.UI.FraudService.OrderQueue>();
            foreach (FraudService.OrderQueue orderQueue in OrderQueue)
            {
                _LoadFraudResults.Add(orderQueue);
            }
        }

    this.collectionViewSites= CollectionViewSource.GetDefaultView(this._LoadFraudResults);
        fraudServiceClient.Close();
    }

}


查看来源

public partial class OrderQueueControl:UserControl {

    private ManageFraudOrderViewModel manageFraudOrderViewModel ;
    private OrderQueue orderQueue;
    private ButtonAction ButtonAction;
    private DispatcherTimer dispatcherTimer;

    public OrderQueueControl()
    {

            LoadOrderQueueForm();
    }

    #region LoadOrderQueueForm
    private void LoadOrderQueueForm()
    {    
    //for binding the first observablecollection
        manageFraudOrderViewModel = new ManageFraudOrderViewModel();
        this.DataContext = manageFraudOrderViewModel;          
    }
    #endregion

    private void cmbWebsite_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        BindItemsSource();
    }

    #region BindItemsSource
    private void BindItemsSource()
    {
        using (OverrideCursor cursor = new OverrideCursor(Cursors.Wait))
        {

            if (!string.IsNullOrEmpty(Convert.ToString(cmbWebsite.SelectedItem)))
            {
                    Wqn.Administration.UI.FraudService.Website website = (Wqn.Administration.UI.FraudService.Website)Enum.Parse(typeof(Wqn.Administration.UI.FraudService.Website),cmbWebsite.SelectedItem.ToString());

        //for binding the second observablecollection*******
                    manageFraudOrderViewModel.LoadOrders(website);
                    this.DataContext = manageFraudOrderViewModel;
        //for binding the second observablecollection*******
            }            
        }
    }
    #endregion   

}

XAML

 ComboBox x:Name="cmbWebsite" ItemsSource="{Binding Sites}"  Margin="5" 
 Width="100" Height="25" SelectionChanged="cmbWebsite_SelectionChanged"
 DataGrid ItemsSource ={Binding Path = LoadFraudResults}

问题区域:

当我调用LoadOrderQueueForm来绑定第一个observableClection和后来的BindItemsSource来绑定2ed observable集合时,一切正常,第一次绑定没问题。

但是,当我再次调用BindItemsSource以通过cmbWebsite_SelectionChanged重新填充基于更改的选定组合值的obseravablecollection时,observalblecollection将填充新值,并且viewmodule中的LoadFraudResults属性将填充新值;但是当我调用datacontext来重新绑定datagrid时,datagrid不会反映更改的值。

换句话说,当在视图的BindItemsSource方法中将datacontext称为2ed时间时,数据网格不会被更改。

 manageFraudOrderViewModel.LoadOrders(website);                    
 this.DataContext = manageFraudOrderViewModel;

manageFraudOrderViewModel值是正确的,但不会使用更改的值重新选择数据网格。

请帮助,因为我在过去2天内遇到这件事情,截止日期临近。

提前致谢

2 个答案:

答案 0 :(得分:5)

尝试使用datagrid.Items.Refresh()!

答案 1 :(得分:3)

是的,ilu2009是正确的。

使用MVVM模式绑定到 DataGrid 并更改 DataGrid.ItemsSource 中的对象需要 DataGrid.ItemsSource.Refresh()反思用户界面。