简体   繁体   中英

Databinding - Listbox doesn't update when updating data after reloading from database

I have a listbox which has items populated from a database. Now I want to update a listbox with a new string value each time I call the Add function.

I did this in 2 ways.

  • I added the new value to the database and updated the ViewModel class where the listbox is binded to. And this works fine. (see AddNewNameFirstWay method below)

  • I added the new value to the database, reloaded the values from the database and updated the ViewModel. But this doesn't work. (see AddNewNameSecondWay method below)

Here is my ViewModel code

public class ViewModel 
{
    private DBContext context = new DBContext("Data source=isostore:/names2.sdf");
    private ObservableCollection<NameTable> nameCollection;

    public ObservableCollection<NameTable> NameCollection
    {
        get
        {
            return nameCollection;
        }
        set
        {
            if (nameCollection != value)
            {
                nameCollection = value;
                NotifyPropertyChanged();
            }
        }
    }

    public void AddNewNameSecondWay(string s)
    {
        NameTable t = new NameTable() { Name = s };
        context.NameDatabaseTable.InsertOnSubmit(t);
        context.SubmitChanges();
        LoadFromDB();
    }

    public void AddNewNameFirstWay(string s)
    {
        NameTable t = new NameTable() { Name = s };
        context.NameDatabaseTable.InsertOnSubmit(t);
        context.SubmitChanges();
        NameCollection.Add(t);
    }

    public void LoadFromDB()
    {
        var query = from i in context.NameDatabaseTable
                    select i;
        NameCollection = new ObservableCollection<NameTable>(query);
    }


    private void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName]string propertyName = null)
    {
        var tmp = PropertyChanged;
        if (tmp != null)
            tmp(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

Here is my XAML binding code

<ListBox ItemsSource="{Binding NameCollection, Mode=OneWay}" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name, Mode=OneWay}" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

It seems to me that second method doesn't work, because the ObservableCollection memory reference changes. If this is correct, how to update the binding properly?

Reason I use the second method is that, I want to make sure all the DB constraints stands true for the values I insert.

You need to notify about the changes in property. Try this way for Framework 4.0,

Mode=OneWayToSource,UpdateSourceTrigger=PropertyChanged

Hope it helps...

Call OnPropertyChanged when you want notification about changes in your collection:

OnPropertyChanged("NameCollection")

Your ViewModel:

public class ViewModel: INotifyPropertyChanged
{
    private DBContext context = new DBContext("Data source=isostore:/names2.sdf");
    private ObservableCollection<NameTable> nameCollection;

    public ObservableCollection<NameTable> NameCollection
    {
        get
        {
            return nameCollection;
        }
        set
        {
            if (nameCollection != value)
            {
                nameCollection = value;
                OnPropertyChanged("NameCollection");
            }
        }
    }

    public void AddNewNameSecondWay(string s)
    {
       // your code
       OnPropertyChanged("NameCollection");
    }


    public event PropertyChangedEventHandler PropertyChanged;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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