简体   繁体   中英

C# BindingList<> not updating a WPF Listbox on changed items

I am on a MVVM C# project.

I want to display a list of objects. I want to add and remove items in this list and ALSO change items in this list.

So I choosed the BindingList<> over the ObservableCollection<>, which would not get noticed if an item has changed. (I also tested the ObservableCollectionEx which is out there in the web, but this has the same behavior like the BindingList for me). But the Listbox is not changing when items are changed. (Adding and removing items is updated in the Listbox)

In my XAML

<ListBox  DisplayMemberPath="NameIndex" ItemsSource="{Binding Profiles}" SelectedItem="{Binding SelectedProfile}">

or alternative with the ItemTemplate

<ListBox  DockPanel.Dock="Right" ItemsSource="{Binding Profiles}" SelectedItem="{Binding SelectedProfile}" Margin="0,10,0,0">
   <ListBox.ItemTemplate>
       <DataTemplate>
           <TextBlock Text="{Binding NameIndex}"/>
       </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

In my ViewModel (ViewModelBase is implementing INotifyPropertyChanged etc)

public class ProfileListViewModel : ViewModelBase
{    
    private BindingList<Profile> profiles;
    public BindingList<Profile> Profiles
    {
        get
        {
            return profiles;
        }
        set
        {
            profiles = value;
            RaisePropertyChanged();
        }
    }

My items are also implementing INotifyPropertyChanged and I am calling OnPropertyChanged("Name") in my Setters.

My model

public class Profile : INotifyPropertyChanged
{
    public Profile(){}

    public int ProfileID { get; set; }

    private string name;
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
            OnPropertyChanged("Name");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Wiring the View with the ViewModel (BindingList is initialized before View)

ProfileListViewModel plvw= new ProfileListViewModel(message.Content);
var profileView = new ProfileListView(plvw);
profileView.ShowDialog();

In the View.xaml.cs

public ProfileListView(ProfileListViewModel plvw)
{
    InitializeComponent();

    DataContext = plvw;
}

When I am changing the name of an object then I get the ListChanged event to which I have subscribted in my ViewModel ( Profiles.ListChanged += Profiles_ListChanged; ) for testing BUT the items in the ListBox are NOT changing.

What am I doing wrong? How can I get a updated Listbox?

Since your DisplayIndex is the computed property NameIndex , you need to call OnPropertyChanged("NameIndex") when its value changes due to a change in other properties, eg:

public string Name
{
    get
    {
        return name;
    }
    set
    {
        name = value;
        OnPropertyChanged("Name");
        OnPropertyChanged("NameIndex");
    }
}

使用 Profiles.ResetBindings() 再次绑定它。

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