How to make ListBox reload ListBoxItems properties

Is there any way how to get an ObservableCollection to activate CollectionChanged?

I have an ObservableCollection object ListBox, so every time I add / remove an item to the collection, the ListBox changes accordingly, but when I change the properties of some objects in the collection, the ListBox still displays the old values.

Even if I modify some properties and then add / delete an object to the collection, nothing happens, I still see the old values.

Is there any other way to do this? I found the INotifyPropertyChanged interface, but I do not know how to use it.

+2
source share
2

. , , INotifyPropertyChanged.

===========

-
============

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;

namespace WpfApplication1
{   
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        Nicknames names;

        public Window1()
        {
            InitializeComponent();
            this.addButton.Click += addButton_Click;
            this.names = new Nicknames();
            dockPanel.DataContext = this.names;
        }

        void addButton_Click(object sender, RoutedEventArgs e)
        {
            this.names.Add(new Nickname(myName.Text, myNick.Text));
        }
}
public class Nicknames : System.Collections.ObjectModel.ObservableCollection<Nickname> { }

public class Nickname : System.ComponentModel.INotifyPropertyChanged
{
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    void Notify(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propName));
        }
    }

    string name;
    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            Notify("Name");
        }
    }
    string nick;
    public string Nick
    {
        get { return nick; }
        set
        {
            nick = value;
            Notify("Nick");
        }
    }
    public Nickname() : this("name", "nick") { }
    public Nickname(string name, string nick)
    {
        this.name = name;
        this.nick = nick;
    }

    public override string ToString()
    {
        return Name.ToString() + " " + Nick.ToString();
    }
  }
}

XAML

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <DockPanel x:Name="dockPanel">
        <TextBlock DockPanel.Dock="Top">
        <TextBlock VerticalAlignment="Center">Name: </TextBlock>
        <TextBox Text="{Binding Path=Name}" Name="myName" />
        <TextBlock VerticalAlignment="Center">Nick: </TextBlock>
        <TextBox Text="{Binding Path=Nick}" Name="myNick" />
        </TextBlock>
        <Button DockPanel.Dock="Bottom" x:Name="addButton">Add</Button>
        <ListBox ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" />
    </DockPanel>
</Grid>

+4

NotifyCollectionChanged - , .

INotifyPropertyChanged. , . , ObservableCollection<Foo>, , Foo INotifyPropertyChanged.

+1

Source: https://habr.com/ru/post/1759522/