WPF INotifyPropertyChanged无法调用另一个类

时间:2015-03-30 14:04:32

标签: c# wpf inotifypropertychanged

我有MainModel:

public class MainModel: INotifyPropertyChanged
{
    public MainModel {
        AllList = new ObservableCollection<MyClassType>();
        GetList();
    }

    private ObservableCollection<MyClassType> _allList;
    public ObservableCollection<MyClassType> AllList 
    {
        get
        {
            return _allList;
        }
        set
        {
            if (value == _allList) return;
            _allList= value;
            OnPropertyChanged("AllList");
        }

    }
     public void GetList()
     {
      AllList = (..get from DataBase)
      }

    public event PropertyChangedEventHandler PropertyChanged;

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

在cs中的MainProgram:

public static MainModel MainModel;    
public MainProgram()
    {
        InitializeComponent();
        MainModel = new MainModel();
        DataContext = MainModel;           
    }

在xaml中的MainProgram:

 <ListBox ItemsSource="{Binding Path=AllList}"/>

它在运行应用程序,列表框视图ListBox项目上工作! 好的,好的。项目有AnotherModel:

class AnotherModel :INotifyPropertyChanged {
...
public void AddNewItem(MyPropertiers properties)
...
}
public Save(){
var prop = new MyPropertiers {name = NameProperty, Size = SizeProperty
AddNewItem(prop);
//!!! 
}
}

哪里!!! - 在DataBase Table中添加了新项目,我想重新加载ListBoxItems,我称之为MainModel.GetList,但是这个GetList不是静态的,如果我调用&#34; new MainModel()。GetList();&#34; - ListBox没有重新加载,为什么?可能不会调用NEW MainModel

0 个答案:

没有答案