Notifying ViewModel that Model Collection changed - c#

I have a model collection that is referenced from a most parts of my code.
public class TraceEntryQueue
{
private readonly Queue<TraceEntry> _logEntries;
public TraceEntryQueue()
{
_logEntries = new Queue<TraceEntry>();
}
public void AddEntry(TraceEntry newEntry)
{
_logEntries.Enqueue(newEntry);
}
public List<TraceEntry> GetAsList()
{
return _logEntries.ToList();
}
}
This collection is presented in one of my views.
public class LoggingViewModel : ViewModelBase
{
private ICollectionView _customers;
public ICollectionView Customers
{
get
{
return _customers;
}
private set
{
_customers = value;
}
}
public LoggingViewModel(TraceEntryQueue traceQueue)
{
Customers = CollectionViewSource.GetDefaultView(traceQueue.GetAsList());
Customers.SortDescriptions.Add(new SortDescription("Index", ListSortDirection.Descending));
Customers.Refresh();
}
}
The question is how I can notify my view to reload the collection when I add new entries via
public void AddEntry(TraceEntry newEntry)
{
_logEntries.Enqueue(newEntry);
}

Use an observable collection instead of a queue. This will automatically notify your view when the collection is updated (add/remove etc.)
public class TraceEntryQueue
{
private readonly ObservableCollection<TraceEntry> _logEntries;
public TraceEntryQueue()
{
_logEntries = new ObservableCollection<TraceEntry>();
}
public void AddEntry(TraceEntry newEntry)
{
_logEntries.Add(newEntry);
}
public ObservableCollection<TraceEntry> GetLogEntries()
{
return _logEntries;
}
}

public class TraceEntryQueue
{
private readonly Queue<TraceEntry> _logEntries;
private readonly ICollectionView _logEntriesView;
public TraceEntryQueue()
{
_logEntries = new Queue<TraceEntry>();
_logEntriesView = CollectionViewSource.GetDefaultView(logEntries.ToList());
}
//....
public void AddEntry(TraceEntry newEntry)
{
_logEntries.Enqueue(newEntry);
_logEntriesView.SourceCollection = logEntries.ToList();
_logEntriesView.Refresh();
}
public ICollectionView GetAsView()
{
return _logEntriesView;
}
}
Use GetAsView and bind directly to your datagrid / listbox / listview.

As #RobJohnson mentionned, you can use an ObservableCollection. However, if you need to create your own collection class, you should implement the INotifyCollectionChanged interface:
public class TraceEntryQueue : INotifyCollectionChanged
{
private readonly Queue<TraceEntry> _logEntries;
public TraceEntryQueue()
{
_logEntries = new Queue<TraceEntry>();
}
public void AddEntry(TraceEntry newEntry)
{
_logEntries.Enqueue(newEntry);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newEntry));
}
public List<TraceEntry> GetAsList()
{
return _logEntries.ToList();
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (CollectionChanged != null)
{
CollectionChanged(this, e);
}
}
}

Related

Refresh datagrid when changes to database are made

I'm writing a wpf application, where I have music albums and corresponding songs. I can add albums and corresponding songs. But now I want to to refresh the view when a change to the database is made. I found many possible solutions, but as I'm new to wpf and c# I don't know which one would suite my code.
In my MainView have an album list and a add button which opens another window where I can add data with a textbox.
AlbumListViewModel
#region Constants
IWindowManager addAlbum = new WindowManager();
IWindowManager addSong = new WindowManager();
private AlbumViewModel _selectedAlbum;
private SongViewModel _selectedSong;
#endregion
#region Constructor
public AlbumListViewModel()
{
Albums = new ObservableCollection<AlbumViewModel>(GetAlbumList());
AddAlbumCommand = new RelayCommand(x => AddAlbum());
AddSongCommand = new RelayCommand(x => AddSong());
}
#endregion
#region Properties
public ICommand AddAlbumCommand { get; private set; }
public ICommand AddSongCommand { get; private set; }
public ObservableCollection<AlbumViewModel> Albums { get; set; }
public AlbumViewModel SelectedAlbum
{
get
{
return _selectedAlbum;
}
set
{
if (_selectedAlbum != value)
{
_selectedAlbum = value;
}
NotifyPropertyChanged("SelectedAlbum");
}
}
public SongViewModel SelectedSong
{
get
{
return _selectedSong;
}
set
{
if (_selectedSong != value)
{
_selectedSong = value;
}
NotifyPropertyChanged("SelectedSong");
}
}
#endregion
#region Methods
public List<AlbumViewModel> GetAlbumList()
{
var controller = new BandManagerController();
return controller.GetAlbumList()
.Select(a => new AlbumViewModel(a))
.ToList();
}
private void AddAlbum()
{
addAlbum.ShowDialog(new AlbumViewModel(new AlbumData()));
}
private void AddSong()
{
addSong.ShowDialog(new SongViewModel(new SongData { AlbumID = SelectedAlbum.AlbumID }));
}
It opens the AlbumView where I add albums to the database.
public class AlbumViewModel : Screen
{
#region Constants
private AlbumData _data;
#endregion
#region Constructor
public AlbumViewModel(AlbumData data)
{
_data = data;
SongListVM = new SongListViewModel(data.AlbumID);
SaveAlbumToDatabase = new RelayCommand(x => AlbumToDatabase(data));
}
#endregion
#region Properties
public SongListViewModel SongListVM { get; set; }
public ICommand SaveAlbumToDatabase { get; private set; }
public string AlbumName
{
get
{
return _data.AlbumName;
}
set
{
if (_data.AlbumName != value)
{
_data.AlbumName = value;
NotifyOfPropertyChange("AlbumName");
}
}
}
public int AlbumID
{
get
{
return _data.AlbumID;
}
set
{
if (_data.AlbumID != value)
{
_data.AlbumID = value;
NotifyOfPropertyChange("AlbumID");
}
}
}
public string AlbumYear
{
get
{
return _data.AlbumYear;
}
set
{
if (_data.AlbumYear != value)
{
_data.AlbumYear = value;
NotifyOfPropertyChange("AlbumYear");
}
}
}
#endregion
#region Methods
public AlbumData AddAlbumEntry(AlbumData albumData)
{
var controller = new BandManagerController();
return controller.AddAlbumEntry(albumData);
}
public void ExecuteCancelCommand()
{
(GetView() as Window).Close();
}
public void AlbumToDatabase(AlbumData data)
{
AddAlbumEntry(data);
ExecuteCancelCommand();
}
#endregion
}
The AddAlbumEntry Method in the ALbumView is in a different class which is the connections to my database. I already use an ObservableCollection but don't know how to tell it the Database was updated.
Thanks in advance!
Just want to answer my question. I just changed my AddAlbum method to use a Deactivated event, to reload the Collection after the Dialog closes like:
private void AddAlbum()
{
var vm = new AlbumViewModel(new AlbumData());
vm.Deactivated += (s, e) => GetAlbumList();
addAlbum.ShowDialog(vm);
}

How to implement and fire an event when a change occurs in a property of `T` in `List<T>` within the owning class

How to implement and fire an event when a change occurs in a property of T in List<T> within the owning class
I mean, not on the collection itself but in a property of T.
Is there any pattern how to do it?
My current code
public class Section
{
public string Title { get; set; }
public List<Question> Questions { get; set; } = new List<Question>();
public int AnsweredQuestion
{
get
{
return Questions.Count(x => x.State != DeviceNodeTechStateEnum.Undefined);
}
}
public int NonAnsweredQuestion
{
get
{
return Questions.Count(x => x.State == DeviceNodeTechStateEnum.Undefined);
}
}
public string QuestionStats
{
get
{
return string.Format("{0}/{1}", AnsweredQuestion, Questions.Count);
}
}
}
public class Question : INotifyPropertyChanged
{
public Guid ID { get; set; }
public string _note { get; set; }
public string Note
{
get
{
return this._note;
}
set
{
if (value != this._note)
{
this._note = value;
NotifyPropertyChanged();
}
}
}
private DeviceNodeTechStateEnum _state { get; set; }
public DeviceNodeTechStateEnum State
{
get
{
return this._state;
}
set
{
if (value != this._state)
{
this._state = value;
NotifyPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Basically I need to know if public DeviceNodeTechStateEnum State has changes in Section class.
I've used this pattern before, where you basically wrap a List, extend it to implement INotifyPropertyChanged, and hook into any methods that Add,Insert or Remove items from the list so that you can wire/unwire the items PropertyChanged event.
public class ItemPropertyChangedNotifyingList<T> : IList<T>, INotifyPropertyChanged where T : INotifyPropertyChanged
{
private List<T> _listImplementation = new List<T>();
public void Add(T item)
{
item.PropertyChanged += ItemOnPropertyChanged;
_listImplementation.Add(item);
}
private void ItemOnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
PropertyChanged?.Invoke(sender, e);
}
public IEnumerator<T> GetEnumerator()
{
return _listImplementation.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable) _listImplementation).GetEnumerator();
}
public void Clear()
{
_listImplementation.ForEach(x => x.PropertyChanged -= ItemOnPropertyChanged);
_listImplementation.Clear();
}
public bool Contains(T item)
{
return _listImplementation.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
_listImplementation.CopyTo(array, arrayIndex);
}
public bool Remove(T item)
{
item.PropertyChanged -= ItemOnPropertyChanged;
return _listImplementation.Remove(item);
}
public int Count => _listImplementation.Count;
public bool IsReadOnly => false;
public int IndexOf(T item)
{
return _listImplementation.IndexOf(item);
}
public void Insert(int index, T item)
{
item.PropertyChanged += ItemOnPropertyChanged;
_listImplementation.Insert(index, item);
}
public void RemoveAt(int index)
{
if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index));
_listImplementation[index].PropertyChanged -= ItemOnPropertyChanged;
_listImplementation.RemoveAt(index);
}
public T this[int index]
{
get => _listImplementation[index];
set => _listImplementation[index] = value;
}
public event PropertyChangedEventHandler PropertyChanged;
}
When handling the PropertyChanged events of this wrapped list, the sender argument will be the instance of the item that raised the event.

How to implement chained/proxied databindings

I have this problem in my mvvm wpf application where I want to create proxied/chained databindings.
My ModelViewController looks like this:
public class ListViewModel
{
public ObservableCollection<Contact> GridData { get; private set; }
public ObservableCollection<Contact> SelectedEntities { get; private set; }
public bool IsSingeselect { get { return SelectedEntities.Count == 1; } }
public bool IsMultiSelect { get { return SelectedEntities.Count > 0; } }
public ObservableCollection<MenuComandModel> ContextMenuItems { get; private set; }
}
GridData and SelectedEntities is bound to a datagrid and works like a charm. I'm using the ContextMenuItems collection to create BarButtonItems for the datagrids contextmenu, this works very good. The MenuComandModel class has a Enabled attribute and I want to bind this on the IsSingeselect or IsMultiSelect attribute to the BarButtonItems member IsEnabled . How would I archive this?
Since you are using DevExpress you can use all the benefits of DevExpress MVVM Framework and their POCO-ViewModels:
using DevExpress.Mvvm.POCO;
//...
public class ListViewModel {
public ObservableCollection<Contact> GridData { get; private set; }
// mark the SelectedEntities property as virtual to be notified on initializing/replacing
public virtual ObservableCollection<Contact> SelectedEntities { get; private set; }
// unsubscribe the CollectionChanged event in changing-callback
protected void OnSelectedEntitiesChanging() {
if(SelectedEntities != null)
SelectedEntities.CollectionChanged -= SelectedEntities_CollectionChanged;
}
// subscribe the CollectionChanged event in changed-callback
protected void OnSelectedEntitiesChanged() {
if(SelectedEntities != null)
SelectedEntities.CollectionChanged += SelectedEntities_CollectionChanged;
UpdateSelectedEntitiesDependencies();
}
void UpdateSelectedEntitiesDependencies() {
// Raise INPC for dependent properties
this.RaisePropertyChanged(x => x.IsSingeselect);
this.RaisePropertyChanged(x => x.IsMultiSelect);
// Raise INPC for dependent properties of child ViewModels
foreach(var item in ContextMenuItems)
item.RaisePropertyChanged(x => x.Enabled);
}
void SelectedEntities_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
if(e.Action == NotifyCollectionChangedAction.Add || e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Reset)
UpdateSelectedEntitiesDependencies();
}
public bool IsSingeselect { get { return SelectedEntities.Count == 1; } }
public bool IsMultiSelect { get { return SelectedEntities.Count > 0; } }
public ObservableCollection<MenuComandViewModel> ContextMenuItems { get; private set; }
}
public class MenuComandViewModel {
public bool Enabled {
get {
var parentViewModel = this.GetParentViewModel<ListViewModel>();
return parentViewModel.IsMultiSelect; // Some implementation
}
}
}
Then you can bind you bar items to the ContextMenuItems collection using the approach described in MVVM Support in DXBars, DXRibbon and GalleryControl help-article.

How do you remove items from several list boxes by removing one item associated with them all?

I want to remove Server1 from it's listbox, i'd want it to remove all the other items in the other listboxes associated with it as well. ("Server-Domain1" and all the "Server1-Domain1-CSR's"). Is there a way to do this?
To "bind" these list boxes i just used:
domainListBox.Items.Add((serverListBox.SelectedItem) + "-" + (this.domainTextbox.Text));
and
csrListBox.Items.Add((domainListBox.SelectedItem) + ("-CSR-1"));
csrListBox.Items.Add((domainListBox.SelectedItem) + ("-CSR-2"));
csrListBox.Items.Add((domainListBox.SelectedItem) + ("-CSR-3"));
If you choose your server from servers listbox, you can remove associated items like this (lets pretend there is some remove button, you selecting domain from listbox and clicking on remove button):
private void removeBtn_Click(object sender, EventArgs e)
{
List<string> items = csrListBox.Items.Cast<string>().ToList();
foreach (string item in csrListBox.Items)
{
Regex regex = new Regex(#"^" + domainListBox.SelectedItem + #"\w*");
Match match = regex.Match(item);
if (match.Success)
{
items.Remove(item);
}
}
csrListBox.DataSource = items;
}
Hope it helps.
Create a class that encapsulates the Server and it's details such as Domains and Csrs. Create a list of Servers and bind it to the first List Box. Then bind the other two List Boxes to the currently selected item of the first List Box. The end result may look like this:
serverListBox.ItemSource = Servers;
domainListBox.ItemSource = (serverListBox.SelectedItem as Server).Domains;
csrListBox.ItemSource = (serverListBox.SelectedItem as Server).Csrs;
This enables you to set the different List Boxes data without writing a lot of code that could make it unmaintainable.
Well, seeing your code, you just have to do something like this when you remove your server:
string server = serverListBox.SelectedItem as string;
serverListBox.Remove(server);
for (int i = domainListBox.Items.Count -1; i >-1; i--)
{
if (domainListBox.Items[i].ToString().StartsWith(server))
{
string domain = domainListBox.Items[i].ToString();
domainListBox.Items.RemoveAt(i);
for (int j = csrListBox.Items.Count-1; j > -1; j--)
{
if (csrListBox.Items[j].ToString().StartsWith(domain))
{
csrListBox.Items.RemoveAt(j);
}
}
}
}
Edit I have tested it now, this should work
a better option would be using observable pattern as
public class ObservableObject<T> : IList, IListSource
{
protected BindingSource src = null;
List<ListControl> Subscribers;
ObservableCollection<T> Data = new ObservableCollection<T>();
public bool IsReadOnly
{
get
{
return false;
}
}
public bool IsFixedSize
{
get
{
return false;
}
}
public int Count
{
get
{
return Data.Count;
}
}
public object SyncRoot
{
get
{
throw new NotImplementedException();
}
}
public bool IsSynchronized
{
get
{
throw new NotImplementedException();
}
}
public bool ContainsListCollection
{
get
{
return true;
}
}
object IList.this[int index]
{
get
{
return Data[index];
}
set
{
Data[index] = (T)value;
}
}
public T this[int index]
{
get
{
return Data[index];
}
set
{
Data[index] = value;
}
}
public ObservableObject()
{
Data.CollectionChanged += Domains_CollectionChanged;
Subscribers = new List<ListControl>();
src = new BindingSource();
src.DataSource = Data;
}
private void Domains_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
src.ResetBindings(false);
}
public virtual void Subscribe(ListBox ctrl)
{
this.Subscribers.Add(ctrl);
//ctrl.DataBindings.Add(new Binding("SelectedValue", src, "Name",
// true, DataSourceUpdateMode.Never));
ctrl.DataSource = src;
}
public int Add(object value)
{
Data.Add((T)value);
return Data.Count - 1;
}
public bool Contains(object value)
{
return Data.Contains((T)value);
}
public void Clear()
{
Data.Clear();
}
public int IndexOf(object value)
{
return Data.IndexOf((T)value);
}
public void Insert(int index, object value)
{
Data.Insert(index, (T)value);
}
public void Remove(object value)
{
Data.Remove((T)value);
}
public void RemoveAt(int index)
{
Data.RemoveAt(index);
}
public void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}
public IEnumerator GetEnumerator()
{
return Data.GetEnumerator();
}
public IList GetList()
{
return Data;
}
}
public class BaseModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _name;
public string Name
{
get { return _name; }
set
{
this._name = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}
}
public class CSR : BaseModel
{
public override string ToString()
{
return Name;
}
}
public class Domain : BaseModel
{
public ObservableObject<CSR> CSRs { get; set; }
public Domain()
{
CSRs = new ObservableObject<CSR>();
}
public override string ToString()
{
return Name;
}
}
public class Server : BaseModel
{
public ObservableObject<Domain> Domains { get; set; }
public Server()
{
Domains = new ObservableObject<Domain>();
}
public override string ToString()
{
return Name;
}
}
public class DataModel : BaseModel
{
public ObservableObject<Server> Servers
{
get; set;
}
public DataModel()
{
Servers = new ObservableObject<Server>();
}
public override string ToString()
{
return Name;
}
}
on Form
DataModel m = new DataModel();
public Form1()
{
...
m.Servers.Subscribe(listBox1);
}
private void listBox2_Click(object sender, EventArgs e)
{
if (listBox2.SelectedIndex >= 0)
{
m.Servers[listBox1.SelectedIndex].Domains[listBox2.SelectedIndex].CSRs.Subscribe(listBox3);
}
}
private void listBox1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex >= 0)
{
m.Servers[listBox1.SelectedIndex].Domains.Subscribe(listBox2);
}
}
you dont have to write code to remove items from forms in this perspective.removing object from model would do

c# ListView not updating when Property Changed

My UI is not updating when more data is added to the ObservableCollection. The console output says A first chance exception of type 'System.NullReferenceException' occurred. Should I be using Inotifycollectionchanged instead? Here is some of the code:
<ListView x:Name="ListView2" ItemsSource="{Binding Source={x:Static d:GrabUserConversationModel._Conversation}, UpdateSourceTrigger=PropertyChanged}" SelectionChanged="ListView1_SelectionChanged">
UserConversationModel.cs
public class UserConversationModel : INotifyPropertyChanged
{
public UserConversationModel()
{
}
public string Name
{ get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string Obj)
{
if (PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(Obj));
}
}
}
MainWindow.xaml.cs
public partial class MainWindow
{
static GrabUserConversationModel grabUserConversationModel;
public MainWindow()
{
InitializeComponent();
...
}
static void AddData()
{
grabUserConversationModel.Conversation.Add(new UserConversationModel { Name = "TestName" });
}
GrabUserConversationModel.cs
class GrabUserConversationModel
{
public static ObservableCollection<UserConversationModel> _Conversation = new ObservableCollection<UserConversationModel>();
public ObservableCollection<UserConversationModel> Conversation
{
get { return _Conversation; }
set { _Conversation = value; }
}
...
your property ObservableCollection<UserConversationModel> Conversation is not implementing the INotifyPropertyChanged
public ObservableCollection<UserConversationModel> Conversation
{
get { return _Conversation; }
set { _Conversation = value; OnPropertyChanged("Conversation");}
}

Categories