力绑定不是我想要的

时间:2013-09-04 04:08:21

标签: c# xaml windows-runtime

我的单向绑定并不像我期望的那样有效。

当我单击按钮添加新的“人物”时,它不会将新输入的人员添加到列表视图中,直到我将其关闭并重新启动应用程序(因此该值将添加到数据库而不是UI )

我做错了什么?我有INotifyPropertyChanged,我有ObservableCollection ......我错过了什么?

我有我的模特:

    public class Person : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string _personName;
        private string _personEmail;
        private string _personPhone;
        private DateTime _personDOB;

        [PrimaryKey, AutoIncrement]
        public int personId { get; set; }
        [MaxLength(25)]
        public string personName {
            get { return _personName; }
            set
            {
                _personName = value;
                OnPropertyChanged("personName");
            }
        }
        [MaxLength(50)]
        public string personEmail { 
            get { return _personEmail; } 
            set
            {
                _personEmail = value;
                OnPropertyChanged("personEmail");
            } 
        }

        [MaxLength(13)]
        public string personPhone { 
            get { return _personPhone; } 
            set
            {
                _personPhone = value;
                OnPropertyChanged("personPhone");
            } 
        }
        public DateTime personDOB {
            get { return _personDOB;}
            set 
            { 
                _personDOB = value;
                OnPropertyChanged("personDOB");
            }
        }
        public Boolean isPersonActive { get; set; }

        public string Summary
        {
            get { return string.Format("{0} - {1} : {2} -- {3}", personName, personEmail, personPhone, personDOB); }
        }

        // Create the OnPropertyChanged method to raise the event 
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }

在我的ViewModel中,我有这个:

public class ChoresVM
{
    private ObservableCollection<win8Chores.Model.databaseTables.Person> _personList;
    public ObservableCollection<win8Chores.Model.databaseTables.Person> personList
    {
        get { return _personList; }
        set { _personList = value; }
    }

...

    public ObservableCollection<win8Chores.Model.databaseTables.Person> selectAllPerson()
    {
        using (var db = new SQLiteConnection(dbPath))
        {
            ObservableCollection<win8Chores.Model.databaseTables.Person> pList = new ObservableCollection<win8Chores.Model.databaseTables.Person>(db.Query<win8Chores.Model.databaseTables.Person>("select personId,personName,personEmail,personDOB from Person"));
            _personList = new ObservableCollection<Model.databaseTables.Person>(db.Query<win8Chores.Model.databaseTables.Person>("select personId,personName,personEmail,personDOB from Person"));
            return _personList;
        }
    }

    public void insertPerson(string name, string email, string phone, DateTime dob, Boolean isActive = true)
    {
        dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "myDB");
        db = new SQLiteConnection(dbPath);
        using (db)
        {
            var p = db.Insert(new win8Chores.Model.databaseTables.Person()
            {
                personName = name,
                personEmail = email,
                personPhone = phone,
                personDOB = dob,
                isPersonActive = isActive

            });
        }
        selectAllPerson();
    }

然后在我的视图中:

    public MainPage()
    {
        this.InitializeComponent();
        VM = new ViewModel.ChoresVM();
        DataContext = VM;
    }

    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        DateTime myDOB = new DateTime(1955, 02, 28);
        VM.insertPerson("test","test@live.com","123-456-7890", myDOB);
    }

我的XAML是这样的:

<ListView HorizontalAlignment="Left" Height="224" Margin="287,344,0,0" VerticalAlignment="Top" Width="740" x:Name="test" DisplayMemberPath="Summary" ItemsSource="{Binding personList,  Mode=OneWay}" />

1 个答案:

答案 0 :(得分:1)

尝试在ChoresVM上实现INotifyPropertyChanged并在初始化集合时引发PropertyChanged(“personList”)(在selectAllPerson()或personList setter中)。这样,将通知itemssource绑定,更改了集合属性并将拾取新集合。你也有一个错字。在Binding中你有“PersonList”(Pascal案例)但你的属性是在驼峰的情况下(personList)