DataGrid未绑定到ObservableCollection

时间:2017-08-16 07:48:54

标签: c# wpf mvvm datagrid observablecollection

我的视图中有一个按钮,可以在ViewModel中执行此方法:

method reference

public void GetAudits(Guid? userId, DateTime? from, DateTime? to, string form) { StringBuilder sCondition = new StringBuilder("WHERE 0=0"); if (userId != null && userId != Guid.Empty) sCondition.Append(string.Format(" AND UserId = '{0}' ", userId)); if (!string.IsNullOrEmpty(form)) sCondition.Append(string.Format(" AND FormName = '{0}' ", form)); string query = string.Format("SELECT * FROM Common.TbHistoryLog {0}", sCondition); Audits = new ObservableCollection<HistoryLog>(oContext.Database.SqlQuery<HistoryLog>(query).ToList()); } 属性:

Audits

这是按钮的处理程序:

 public ObservableCollection<HistoryLog> Audits
    {
        get
        {
            return audits;
        }
        set
        {
            audits = value;
        }
    }
    ObservableCollection<HistoryLog> audits;

但是当我点击按钮时,dataGrid不会使用Audits集合进行更新:

private void BtnSearch(object sender, RoutedEventArgs e)
        {
            var userId = ((TbUsers)cmbUsers.SelectedItem)?.UserId;
            var from = dtFromDate.Value;
            var to = dtToDate.Value;
            var form = ((BlCommon.TbObjects)cmbForms.SelectedItem)?.ObjectRealName;
            using (ClsUserTransactions oUserTrans = new ClsUserTransactions())
            {
                oUserTrans.GetAudits(userId, from, to, form);
            }
        }

当我调试时,我注意到没有点击getter块

2 个答案:

答案 0 :(得分:1)

为什么不简单地清除并重新填充相同的ObservableCollection?:

public void GetAudits(Guid? userId, DateTime? from, DateTime? to, string form)
{
    StringBuilder sCondition = new StringBuilder("WHERE 0=0");

    if (userId != null && userId != Guid.Empty)
        sCondition.Append(string.Format(" AND UserId = '{0}' ", userId));

    if (!string.IsNullOrEmpty(form))
        sCondition.Append(string.Format(" AND FormName = '{0}' ", form));


    string query = string.Format("SELECT * FROM Common.TbHistoryLog {0}", sCondition);
    if (Audits != null)
    {
        Audits.Clear();
        var newItems = oContext.Database.SqlQuery<HistoryLog>(query).ToList();
        if (newItems != null)
            foreach (var newItem in newItems)
                Audits.Add(newItem);
    }
}

其次,这会创建ClsUserTransactions新实例

ClsUserTransactions oUserTrans = new ClsUserTransactions())

您需要将项目添加到DataGrid绑定的项目中,并且在填充其Audits集合后,您当然不应立即处置该实例...:

private void BtnSearch(object sender, RoutedEventArgs e)
{
    var userId = ((TbUsers)cmbUsers.SelectedItem)?.UserId;
    var from = dtFromDate.Value;
    var to = dtToDate.Value;
    var form = ((BlCommon.TbObjects)cmbForms.SelectedItem)?.ObjectRealName;
    ClsUserTransactions oUserTrans = gvHistory.DataContext as ClsUserTransactions;
    oUserTrans.GetAudits(userId, from, to, form);
}

答案 1 :(得分:0)

您的ViewModel应该实现INotifyPropertyChanged接口,然后在Audits setter上通知属性更改:

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
...

-

public ObservableCollection<HistoryLog> Audits
{
    get
    {
        return audits;
    }
    set
    {
        audits = value;
        OnPropertyChanged("Audits");
    }
}
ObservableCollection<HistoryLog> audits;