{"id":702,"date":"2023-12-08T20:19:54","date_gmt":"2023-12-09T02:19:54","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=702"},"modified":"2023-12-08T20:19:54","modified_gmt":"2023-12-09T02:19:54","slug":"observablecollection-in-dotnet","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/observablecollection-in-dotnet\/","title":{"rendered":"ObservableCollection<T> in dotnet"},"content":{"rendered":"\n<p><strong>Definition<\/strong>: <\/p>\n\n\n\n<p><code>ObservableCollection&lt;T><\/code> is a class in the .NET Framework that provides a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed. <\/p>\n\n\n\n<p>It is part of the <code>System.Collections.ObjectModel<\/code> namespace.<\/p>\n\n\n\n<p>Key features of <code>ObservableCollection&lt;T><\/code>:<\/p>\n\n\n\n<ol>\n<li><strong>Dynamic Updates:<\/strong> <code>ObservableCollection&lt;T&gt;<\/code> automatically notifies subscribers (such as UI elements) when items are added, removed, or when the entire collection is refreshed. This makes it particularly useful in scenarios where you want the user interface to automatically reflect changes in the underlying data.<\/li>\n\n\n\n<li><strong>Designed for Data Binding:<\/strong> It&#8217;s commonly used in data binding scenarios, especially in WPF and other XAML-based frameworks, where you want the UI to stay in sync with changes in the data.<\/li>\n\n\n\n<li><strong>Events:<\/strong> <code>ObservableCollection&lt;T&gt;<\/code> provides events such as <code>CollectionChanged<\/code> that allow you to subscribe to changes in the collection.<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<p>An example of how <code>ObservableCollection&lt;T><\/code> in a WPF application might be used:<\/p>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#5E69FF\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-clike\">using System.Collections.ObjectModel;\nusing System.ComponentModel;\nusing System.Windows;\n\npublic class ViewModel : INotifyPropertyChanged\n{\n    private ObservableCollection&lt;string> _items;\n\n    public ObservableCollection&lt;string> Items\n    {\n        get { return _items; }\n        set\n        {\n            if (_items != value)\n            {\n                _items = value;\n                OnPropertyChanged(nameof(Items));\n            }\n        }\n    }\n\n    public ViewModel()\n    {\n        \/\/ Initialize the collection and add some items\n        Items = new ObservableCollection&lt;string>\n        {\n            \"Item 1\",\n            \"Item 2\",\n            \"Item 3\"\n        };\n\n        \/\/ Subscribe to the CollectionChanged event\n        Items.CollectionChanged += (sender, e) =>\n        {\n            \/\/ Handle changes to the collection (additions, removals, etc.)\n            MessageBox.Show(\"Collection changed!\");\n        };\n    }\n\n    public event PropertyChangedEventHandler PropertyChanged;\n\n    protected virtual void OnPropertyChanged(string propertyName)\n    {\n        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));\n    }\n}<\/code><\/pre><\/div><\/div>\n\n\n\n<p>In this example:<\/p>\n\n\n\n<ul>\n<li><strong><code>ViewModel<\/code> <\/strong>has a property called <code>Items<\/code> of type <code>ObservableCollection&lt;string><\/code>.<\/li>\n\n\n\n<li>The <strong>constructor <\/strong>initializes the collection and adds some items.<\/li>\n\n\n\n<li>The <strong><code>CollectionChanged<\/code> <\/strong>event is subscribed to, so whenever the collection changes, a message box is displayed.<\/li>\n<\/ul>\n\n\n\n<p>In a WPF application, you might bind this collection to a <strong><code>ListBox<\/code> <\/strong>or another UI control, and the UI would automatically update when items are added or removed from the collection. <\/p>\n\n\n\n<p>This is because the <strong><code>ListBox<\/code> <\/strong>is notified of changes in the <code>ObservableCollection<\/code>, and it refreshes itself accordingly.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Definition: ObservableCollection&lt;T> is a class in the .NET Framework that provides a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed. It is part of the System.Collections.ObjectModel namespace. Key features of ObservableCollection&lt;T>: An example of how ObservableCollection&lt;T> in a WPF application might be used: In this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":257,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[41,51,32,56,52],"tags":[53,115,58,54],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/702"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=702"}],"version-history":[{"count":1,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/702\/revisions"}],"predecessor-version":[{"id":703,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/702\/revisions\/703"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/257"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}