How to trigger an event form parent to child? RRS feed

  • Question

  • I have a column class with 2 properties Name and Value, and another class(ColumnList) which inherit it as a collection, I want to raise an event from parent class to child class means change in column value should be triggered in ColumnList class.

    public class Column { private string colName; private string colValue; public string Name { get { return colName; } set { colName = value; if (ColumnValueChangedEvent != null) ColumnValueChangedEvent(null, null); } } public string Value { get { return colValue; } set { colValue = value; if (ColumnValueChangedEvent != null) ColumnValueChangedEvent(null, null); } } public event ColumnValueChangedDelegate ColumnValueChangedEvent; public delegate void ColumnValueChangedDelegate(object sender, EventArgs e); } public class AddColumn : List<Column> { public AddColumn() : base() { } }

    public class test()


    AddColumn colList = new AddColumn();

    //I need an event in this class when value of one item is changed.


    Thursday, August 8, 2019 10:46 AM

All replies

  • "I want to raise an event from parent class to child class means change in column value should be triggered in ColumnList class."

    What do you mean by this. Changing a column's value should raise an event in the `Column` type which `AddColumn` could listen for. It wouldn't go the other way. You wouldn't change a column's value from `AddColumn`and that raise an event on a `Column`. Syntactically I wouldn't even image how that would work.

    var items = new AddColumn();
    items.Add(new Column() { Name = "Col1" });
    //This is setting the Value property on Column
    items["Col1"].Value = 10;
    Note also that `AddColumn` probably needs to be more complex than just a list of columns. At a minimum you should probably not allow arbitrary columns to be added. What happens if the caller adds `null`? What about if the caller adds a column with the same name as an existing column? If this is an internal type then just stick with `List<Column>`.

    Michael Taylor

    Thursday, August 8, 2019 2:09 PM
  • Your column class seems to be the same as the KeyValuePair<TKey,TValue> Struct (System.Collections.Generic) class. If you are familiar with the KeyValuePair class then it will help if you can explain why you cannot use it. There are .Net collection classes that use the KeyValuePair class.

    Can you use the ObservableCollection<T> class?

    Sam Hobbs

    Thursday, August 8, 2019 4:09 PM
  • Can you use the ObservableCollection<T> class?
    I tried with this one, an event is triggered while adding removing a column from the AddColumn object but unable to catch/trigger when value of one item is changed. 

    KeyValuePair<TKey,TValue> Struct this could make me trouble while serialization and de-serialization.  

    please check code i updated;

    Friday, August 9, 2019 12:12 PM
  • ObservableCollection is simply a collection that raises events when the collection is modified. Changes to items within a collection are not supported by any current type as it is very specific to the type you are working with. If you want to raise events when the properties on a type are changed then implement the INotifyPropertyChanged event on the type in question (your `Column` type in this case). You then have to update each of your property setters to determine if the property value has actually changed and, if so, raise the event. The link I posted has a proto-typical implementation of this interface.

    To actually use the interface you have to explicitly handle the event in code that might care. In the case of WPF this is done automatically but Winforms and other apps you have to do it explicitly.

    Michael Taylor

    Friday, August 9, 2019 1:30 PM
  • I admit that I am not as familiar with ObservableCollection and INotifyPropertyChanged as I should be. I should have said INotifyPropertyChanged. You imply however that ObservableCollection  is useless here. It might be useless for the question asked but it still seems useful for the application.

    Also, the implication is that INotifyPropertyChanged creates more work. Yet INotifyPropertyChanged seems to be what the designers of .Net intended to be used to satisfy the requirements here.

    As for Windows Forms and WPF, the question says nothing about either. The question should be in the forum corresponding to the relevant UI.

    Sam Hobbs

    Friday, August 9, 2019 5:23 PM
  • Hi Madan Mishra, 

    Thank you for posting here.

    For your question, you try to use the ObservableCollection<T> class to trigger an event form parent to child.

    I have some questions to confirm with you.

    What's the type of your application? Winform? WPF? Why do you want to trigger an event form parent to child?

    Please provide more details about your problem, and it will help us to analyze your problem.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Monday, August 12, 2019 7:47 AM