简体   繁体   English

我无法将DataGrid绑定到ObservableCollection

[英]I can't binding DataGrid to ObservableCollection

I can't understand what's happening here. 我不明白这里发生了什么。 I have two public properties in my ViewModel: 我的ViewModel有两个公共属性:

public ObservableCollection<vw_ClientesFull> MyClients { get; set; }

private IEnumerable<vw_ClientesFull> _clients;
public IEnumerable<vw_ClientesFull> Clients
        {
            get
            {
                return _clients;
            }
            set
            {
                _clients= value;
                OnPropertyChanged("Clients");
            }
        }

Then I have a method to refresh both of them: 然后,我有一种刷新它们的方法:

private void RefreshClientes()
    {
        this.serviceClient.Clientes_ListarCompleted += (s, e) =>
        {                
            Clients = e.Result;
            MyClients = new ObservableCollection<vw_ClientesFull>(Clients);
        };            
        this.serviceClient.Clientes_ListarAsync(_sRazonSocial, VendedorSel, TransporteSel, _nID, bInactivos);
    }

Them i bind my dataGrid to show the information. 我将它们绑定到dataGrid以显示信息。 If I do: 如果我做:

ItemsSource="{Binding Path=Clients}"

If works perfect, but if i do: 如果工作完美,但如果我这样做:

ItemsSource="{Binding Path=MyClients}"

Nothing is show! 什么都没显示! Why? 为什么? Doesn't ObservableCollection fire onPropertyChange Automaticaly? ObservableCollection不会自动触发onPropertyChange吗?

Thanks for the help!!! 谢谢您的帮助!!!

UPDATE 更新

So if i need to fire the OnPropertyChange manualy, why this work without it? 因此,如果我需要手动触发OnPropertyChange,为什么没有它就可以工作?

public ObservableCollection<Vendedores> Vendedores { get; set; }

private void CargarVendedores()
    {        
        Vendedores = new ObservableCollection<Vendedores>(this.serviceClient.GetVendedores());                           
        this.VendedorSel = this.Vendedores.FirstOrDefault();
    }

If i bind a combobox like this: 如果我这样绑定一个组合框:

ItemsSource="{Binding Path=Vendedores}"

Work without the OnPropertyChange! 无需OnPropertyChange即可工作! Why! 为什么!

This problem is due to a misconception. 此问题是由于误解造成的。 ObservableCollection does not raise PropertyChanged, (which happens when the entire property is reassigned) when you replace it, but rather CollectionChanged (which is raised when items are added or removed). 的ObservableCollection 提高的PropertyChanged,当你更换(当整个财产被重新分配它发生的),而是CollectionChanged(在将项目添加或删除其升高)。 You still need to raise PropertyChanged if you plan to reassign the whole object. 如果您打算重新分配整个对象,则仍然需要提高PropertyChanged。

设置MyClients的值时必须引发PropertyChanged事件,这与对Clients所做的相同。

Yes, ObservableCollection implements INotifyPropretyChanged. 是的,ObservableCollection实现了INotifyPropretyChanged。 However, it isn't going to help you here :) 但是,它不会在这里帮助您:)

ObservableCollection is special because it implements INotifyCollectionChanged . ObservableCollection很特别,因为它实现了INotifyCollectionChanged In other words, it raises an event when items are added to or removed from the underlying collection. 换句话说,当将项目添加到基础集合或从基础集合中删除时,它引发一个事件。 It also implements INotifyPropertyChanged, so anything bound to a property of the collection will get updated. 它还实现了INotifyPropertyChanged,因此绑定到集合属性的所有内容都将得到更新。

You are changing the variable itself though (setting to a new instance no less). 不过,您正在更改变量本身(至少设置为一个新实例)。 This requires that the "instance" of the ObservableCollection property raise the event. 这要求ObservableCollection 属性的“实例”引发事件。 In other words, you need: 换句话说,您需要:

private ObservableCollection<vw_ClientesFull> myClients;
public ObservableCollection<vw_ClientesFull> MyClients 
{ 
    get { return myClients; }
    set
    {
       myClients = value;
       OnPropertyChanged("MyClients"); 
}

In your update, the binding hasn't fired yet (you set in the constructor) so it gets the correct list. 在您的更新中,绑定尚未触发(您在构造函数中设置),因此它会获取正确的列表。 Subsequent changes to the list wouldn't work, however. 但是,列表的后续更改将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM