2

I'm new to C# and I'm trying to get a change in my datagrid to trigger a method in my main.

I've got my class:

public class siteMatch : INotifyPropertyChanged
{
    public string SelectedTag
    {
        get { return _SelectedTag; }
        set
        {
            if (value != _SelectedTag)
            {
                _SelectedTag = value;
                OnPropertyChanged(_SelectedTag);
            }
        }

    }
    private string _SelectedTag;
    protected void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, e);
    }

    protected void OnPropertyChanged(string propertyName)
    {
        OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }
}

And I've got this in my main class:

public partial class MainWindow : Window
{
    public ObservableCollection<siteMatch> sitesMatched = new ObservableCollection<siteMatch>();

    public MainWindow()
    {
        InitializeComponent();
        sitesMatched.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(items_CollectionChanged);
    }

    static void items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        foreach (INotifyPropertyChanged item in e.OldItems)
            item.PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);

        foreach (INotifyPropertyChanged item in e.NewItems)
            item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
    }

    static void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        //throw new NotImplementedException();
    }
}

Am I missing something here? The items_CollectionChanged method doesn't get triggered when I change something on the datagrid. I have a feeling that I haven't subscribed to the event properly. The OnPropertyChanged method gets triggered correctly but nothing happens after that.

Any help will be greatly appreciated.

Thanks,

Edit: I should point out that I only have access to .Net 4

Szabolcs Dézsi
  • 8,743
  • 21
  • 29
Phillip Le
  • 23
  • 3
  • This OnPropertyChanged(_SelectedTag); should be OnPropertyChanged("_SelectedTag"); – Exxoff Feb 16 '16 at 16:54
  • @Exxoff Shouldn't it be OnPropertyChanged("SelectedTag")? Since SelectedTag is the public property. _SelectedTag is a private datamember. – Gábor Birkás Feb 16 '16 at 19:19
  • @GáborBirkás OK, but when omitting the quotation marks, isn't he sending the value of the property to the OnPropertyChanged method instead of the property name itself? – Exxoff Feb 16 '16 at 21:15
  • @GáborBirkás I switched it over, thanks for that. – Phillip Le Feb 17 '16 at 08:58

1 Answers1

0

It is my understanding that the Collection Changed is only triggered on creation of the collection, or in addition or deletion, but not when you change something inside the collection.

What you want to accomplish can be done with the events on the Datagrid itself,

e.g. by using the DataGridView.CellValueChanged Event.

Another solution is for you to expand the ObservableCollection class. Have a look here : https://stackoverflow.com/a/5256827/3042778

Community
  • 1
  • 1
Konstantine
  • 71
  • 1
  • 9
  • Hey mate, I using WPF so I'm using DataGrid instead of DataGridView and DataGrid doesn't have a CellValueChanged event. Thanks for the attempt though. – Phillip Le Feb 17 '16 at 08:09
  • And I think you're correct about the Collection Changed thing, time to figure out how to subscribe to the Property Changed events. – Phillip Le Feb 17 '16 at 08:17