Skip to main content
deleted 30 characters in body
Source Link
CSharpie
  • 9.5k
  • 5
  • 50
  • 78

Your Testclass needs to implement INotifyPropertyChanged

public class TestClass : INotifyPropertyChanged
{
    string _name;
    
    public string Name
    {
        get { return _name;}
        set 
        {
              _name = value;
              _notifyPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
   
    private void _notifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        
    }

    public TestClass(string name)
    {
        this.Name = name;
    }

    public override string ToString()
    {
        return this.Name;
    }
}

However this only works if you use Columns that do not rely on the ToString() but bind the property Name

This can be done by altering your code:

somewhere in class declare

BindingList<TestClass> _dataSource = new BindingList<TestClass>();

In initializeComponent write

listBox1.DataSource = _dataSource;

Then do all operations on _dataSource instead of Listbox.

Your Testclass needs to implement INotifyPropertyChanged

public class TestClass : INotifyPropertyChanged
{
    string _name;
    
    public string Name
    {
        get { return _name;}
        set 
        {
              _name = value;
              _notifyPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
   
    private void _notifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        
    }

    public TestClass(string name)
    {
        this.Name = name;
    }

    public override string ToString()
    {
        return this.Name;
    }
}

However this only works if you use Columns that do not rely on the ToString() but bind the property Name

Your Testclass needs to implement INotifyPropertyChanged

public class TestClass : INotifyPropertyChanged
{
    string _name;
    
    public string Name
    {
        get { return _name;}
        set 
        {
              _name = value;
              _notifyPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
   
    private void _notifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
    }

    public TestClass(string name)
    {
        this.Name = name;
    }

    public override string ToString()
    {
        return this.Name;
    }
}

However this only works if you use Columns that do not rely on the ToString() but bind the property Name

This can be done by altering your code:

somewhere in class declare

BindingList<TestClass> _dataSource = new BindingList<TestClass>();

In initializeComponent write

listBox1.DataSource = _dataSource;

Then do all operations on _dataSource instead of Listbox.

Source Link
CSharpie
  • 9.5k
  • 5
  • 50
  • 78

Your Testclass needs to implement INotifyPropertyChanged

public class TestClass : INotifyPropertyChanged
{
    string _name;
    
    public string Name
    {
        get { return _name;}
        set 
        {
              _name = value;
              _notifyPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
   
    private void _notifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        
    }

    public TestClass(string name)
    {
        this.Name = name;
    }

    public override string ToString()
    {
        return this.Name;
    }
}

However this only works if you use Columns that do not rely on the ToString() but bind the property Name