Click here to Skip to main content
15,899,023 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have a class that extends
C#
List<T>
and has a generic property

C#
public class myClass : System.Collections.Generic.List<String>, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    decimal _myProperty;

    public decimal myProperty
    {
       get { return _myProperty; }
       set
       {
           PropertyChanged(this, new PropertyChangedEventArgs("myProperty"));
           _myProperty = value;
       }
}


I bind "myProperty" of an instance of "myClass" to "Value" property of "NumericUpDown"

C#
public partial class myForm : Form
{
    myClass x = new myClass();

    public form_RDG3000_1Ch(RDG3000_1Ch rdg)
    {
        InitializeComponent();

        myNumericUpDown.DataBindings.Add("Value", x, "myProperty", false, DataSourceUpdateMode.OnPropertyChanged);
    }


When I show myForm appears "ArgumentException"

{"Impossibile stabilire l'associazione alla proprietà o alla colonna myProperty di DataSource.
Nome parametro: dataMember"}


Unable to establish the association to the DataSource property or myProperty column.
Parameter name: dataMember


What I have tried:

Nothing ......................
Posted
Updated 4-Jul-19 21:08pm
v4

Or, you can subscribe to the "value changed" event for the numeric up down control (via its host class), and get notified / updated that way. Same "change" cycle.
 
Share this answer
 
Welcome to the "hidden surprises" of WinForms :)

Your code should be valid: it's the fact your Class subclasses List<string> is the problem. And, I have been too lazy (for years) to figure out why :)

Change your class to:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace TestStruct
{
    public class MyClass : INotifyPropertyChanged
    {

        List<string> MyStrings = new List<string>();

        public event PropertyChangedEventHandler PropertyChanged;

        decimal _myProperty;

        public decimal myProperty
        {
            get { return _myProperty; }
            set
            {
                if (PropertyChanged != null) NotifyPropertyChanged(); 
                _myProperty = value;
            }
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
 
Share this answer
 
v2
Comments
BillWoodruff 5-Jul-19 21:18pm    
@openLG You may wish to inherit from ObservableCollection<T> which implements interfaces INotifyCollectionChanged, and INotifyPropertyChanged

https://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900