0

I have an ObservableDictionary that I bind to a ListView. The ObservableDictionary contains ClientViewModels which are ViewModel wrappers for my Client model that contain UI-only properties.

The problem I am having, is let's say a Client connects/disconnects and an event is raised. If I want to update or remove the ClientViewModel from the ObservableCollection, I'd have to do some sort of LINQ hackery to search each ViewModel in the ObservableCollection, and then check if the Client inside the ClientViewModel is equal to the Client that connects/disconnected, and then modify the ViewModel.

Is there a more efficient way? Like having some sort of ObservableDictionary that can be accessed O(1)?

public ObservableCollection<ClientViewModel> ClientViewModels { get; } = new();

public void OnClientConnected(object sender, Client e)
{
    ClientViewModels[...]?
}
public class ClientViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public Client Client { get; }

    private bool _online;
    public bool Online
    {
        get { return this._online; }
        set
        {
            this._online = value;
            this.NotifyPropertyChanged(nameof(Online));
        }
    }

    public ClientViewModel(Client client)
    {
        Client = client;
    }

    public void NotifyPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.