Home > Software engineering >  Is it possible to attach an event to a custom collection and modify the ui on changes to the collect
Is it possible to attach an event to a custom collection and modify the ui on changes to the collect

Time:08-31

Let's say, for example, I have a class called Error, and properties of 'Error' include ErrorMessage, ErrorCode, etc. When a new instance of Error has been created, the instance is added to a collection, ErrorCollection:ICollection. Would it be possible to change the UI once it's added? If so, how can this be done without causing problems with Threads

Note: an instance of the Error class would be created asynchronously.

Crude visual example:

public class ErrorCollection:ICollection<Error>
    {
        private readonly ICollection<Error> _collection = new Collection<Error>();
        public EventArgs e = null;
        public void Add(Error item)
        {
            _collection.Add(item);
            if(AddEventHandler != null)
            {
                AddEventHandler(this, e);
            }
        }
        public event EventHandler AddEventHandler;
        public void OnAdd(object sender, EventArgs e)
        {
            //Do some more stuff here including updating UI
        }
        ...
    }
public class Error
    {
        public Error()
        {                        
        }

        private Task<Error> InitializeErrorAsync()
        {
            return Task.Run(() =>
            {
                //Do some stuff that can take a while
                Thread.Sleep(5000);
                return this;
            });            
        }

        public static async Task<Error> CreateError()
        {
            Error error = new Error();
            return await error.InitializeErrorAsync();
        }
    }
public partial class MainWindow : Window
    {
        ErrorCollection errorCollection = new ErrorCollection();        
        public MainWindow()
        {
            InitializeComponent();
            errorCollection.AddEventHandler  = new EventHandler(errorCollection.OnAdd);                        
        }
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            errorCollection.Add(await Error.CreateError());            
        }
    }

Edit: Let me also clarify, in my example, I was using Error as an example class. Not to say I won't use it for Errors, but I'm looking for a general solution.

CodePudding user response:

there is a class that notifies the viewmodel and the model whenever errors occur it also allows the ui to know an then it changes apparence regarding that check this post https://social.technet.microsoft.com/wiki/contents/articles/19490.wpf-4-5-validating-data-in-using-the-inotifydataerrorinfo-interface.aspx

CodePudding user response:

By using an ObservableCollection instead of ICollection. ObservableCollection implements INotifyCollectionChanged, which can be used in combination with ItemsSource to automatically update the UI when the collection changes. No need to use events

  • Related