【问题标题】:Why is ObservableCollection not updating the UI data grid?为什么 ObservableCollection 不更新 UI 数据网格?
【发布时间】:2019-11-14 04:08:08
【问题描述】:

我正在尝试在我的应用中显示客户。我已经将 WPF 与 MVVM Light 一起使用。 当我将视图切换到客户时,我有空的数据网格。为什么不加载?

我的 ShellViewModel 继承自 BaseViewModel。

我正在将用户从 MainViewModel 发送到我的 ClientsViewModel。

ClientsViewModel:

public class ClientsViewModel : ShellViewModel
{
    public ObservableCollection<Customer> Customers { get; set; }

    public Customer customer { get; set; }

    public ClientsViewModel()
    {
        SetView("Klienci");
        Messenger.Default.Register<UserMessage>(this, (message) =>
        {
            try { User = message.User;
                GetCustomers();
            }
            catch (System.InvalidOperationException e) { }
        });

    }

    private void GetCustomers()
    {
        Customers = new ObservableCollection<Customer>();

        using (var context = new mainEntities())
        {
            var result = context.customerassigned.Where(c => c.user_id == User.Id).Include(c => c.customer).Where(c=>c.customer.status_id == 1);
            foreach (var item in result)
            {
                Customers.Add(new Customer(item.customer.id, item.customer.name, item.customer.status_id));
            }

        }
    }
}

GetCustomers() 方法工作正常。 我在 ClientsView 中的数据网格:

<DataGrid Name="grdCustomers" SelectedItem="{Binding ClientsView.customer}" ItemsSource="{Binding ClientsView.Customers, Mode=OneWay}" AutoGenerateColumns="true" Height="500"></DataGrid>

我应该改变什么?

【问题讨论】:

  • AutoGenerateColumns="tue" 可能是 o 型吗?
  • 除了 ItemsSource Binding 不需要是 TwoWay 之外,ItemsSource="{Binding ClientsView.Customers} 中使用的声明的 ClientsView 属性在哪里。你是如何设置视图的 DataContext 的?
  • 你不需要INotifypropertychangedINotifyCollectionchanged接口与MVVM轻工具包一起工作吗?自己从来没用过……
  • 您可以使用 spy 来检查您的属性是否为空。
  • 很有可能,您的GetCustomers 方法更改了Customers 属性(分配一个新的 ObservableCollection 实例)绑定建立之后。由于Customers 属性设置器在Customers 属性更改时不会发出属性更改通知(它不做任何事情,它只是你老的琐碎的自动实现的属性),绑定机制不会知道该属性已更改,因此不会将新的 ObservableCollection 实例转发到 DataGrid。 (您的 customer 属性也存在类似问题)

标签: c# wpf mvvm mvvm-light observablecollection


【解决方案1】:

我尝试过这样的事情:

private ObservableCollection<Customer> customers;
    public ObservableCollection<Customer> Customers
    {
        get
        {
            return customers;
        }
        set
        {
            if (customers == value)
                return;
            customers = value;
            RaisePropertyChanged("Customers");
        }
   }

但它也不起作用。

如果我返回 main 并再次访问 cutomers,则客户在 datagrid 中。 简单:

public ObservableCollection<Customer> Customers { get; set; }

效果一样

【讨论】:

    【解决方案2】:

    不要创建新的 ObservableCollection 实例,只需创建一次并根据需要填充它。

    public ObservableCollection<Customer> Customers { get; } = new ObservableCollection<Customer>();
    
    ...
    
    private void GetCustomers()
    {
        Customers.Clear();
    
        using (var context = new mainEntities())
        {
            var result = context.customerassigned.Where(c => c.user_id == User.Id).Include(c => c.customer).Where(c=>c.customer.status_id == 1);
            foreach (var item in result)
            {
                Customers.Add(new Customer(item.customer.id, item.customer.name, item.customer.status_id));
            }
    
        }
    }
    

    另外,请注意上面的 Clemens cmets。

    你需要

    1) 创建 ClientsViewModel 的实例并将其分配给 View 的 DataContext。

    2) 仅绑定到 ClientsViewModel 类的公共属性 - 在这种情况下为 ItemsSouce ==> 客户,而不是 ClientsView.Customers。

    【讨论】:

      猜你喜欢
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-05
      • 2016-05-03
      • 2019-03-16
      • 1970-01-01
      • 2011-01-25
      相关资源
      最近更新 更多