Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ItemsSource binding do not update values

Tags:

c#

wpf

xaml

I need to make list of items. I binded collection of users to listbox. Everything works quite well, but items in listbox aren't updated in real time. They aren't updated at all by this binding. So when I remove any user from the list, listbox isn't updated even if its source is correctly changed.

Source is located in data view model at path DataViewModel.Instance.AllUsers; Whenever I add to this list new item or remove one, layout does not update. Other bindings work well. I tried to update listbox layout, to raise event of source update, other way of adding/removing items, but nothing worked.

I tried to debug binding, but I have too many bindings to find the error.

Thanks in advance for any useful advice.

Listbox:

<ListBox x:Name="ListboxUsers" ItemsSource="{Binding Path=AllUsers, Mode=OneWay}" Grid.Column="1" Margin="0" Grid.Row="5" Background="DimGray" BorderThickness="0" Visibility="Hidden" SelectionChanged="ListboxUsers_SelectionChanged"/>

Code-behind:

CatalogueGrid.DataContext = DataViewModel.Instance;    //whole view model added as datacontext

DataViewModel class:

public class DataViewModel : INotifyPropertyChanged
{
    private static DataViewModel _dataViewModel;

    private Collection<UserModel> allUsers;
    public Collection<UserModel> AllUsers
    {
        get
        {
            return allUsers;
        }
        set
        {
            allUsers = value;
            NotifyPropertyChanged("AllUsers");
        }
    }


    private DataViewModel()
    {
        AllUsers = new Collection<UserModel>();
    }    

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string info)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(info));
        }
    }
    .
    .
    .

}
like image 893
Qerts Avatar asked Oct 30 '25 00:10

Qerts


1 Answers

use ObservableColLection instead if Collection wich implements the INotifyCollectionChanged Interface :

private ObservableCollection<UserModel> allUsers;
public ObservableCollection<UserModel> AllUsers
{
    get
    {
        return allUsers;
    }
    set
    {
        allUsers = value;
        NotifyPropertyChanged("AllUsers");
    }
}
like image 75
SamTh3D3v Avatar answered Oct 31 '25 16:10

SamTh3D3v