Skip to content

Instantly share code, notes, and snippets.

@reicheltp
Created October 22, 2017 16:14
Show Gist options
  • Select an option

  • Save reicheltp/e2fa08c8401ce62d0dca8dfe98f261a5 to your computer and use it in GitHub Desktop.

Select an option

Save reicheltp/e2fa08c8401ce62d0dca8dfe98f261a5 to your computer and use it in GitHub Desktop.
INPC INCC
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var list = new ObservableCollection<int>();
list.CollectionChanged += (_, e) =>
{
Console.WriteLine($"CollectionChanged: {e.Action} - {string.Join(" - ", e.NewItems?.OfType<int>() ?? Enumerable.Empty<int>())} | {string.Join(" - ", e.OldItems?.OfType<int>() ?? Enumerable.Empty<int>())}");
};
list.Add(5);
list.Add(6);
list.Add(7);
list.Add(8);
list.RemoveAt(2);
list.Move(0, 2);
Console.WriteLine(string.Join(" - ", list));
// Property Changed
var vm = new ViewModel();
vm.PropertyChanged += (_, e) =>
{
Console.WriteLine($"PropertyChanged: {e.PropertyName}");
};
vm.Text = "Hallo";
vm.Text = "Hallo";
vm.Text = "Hallo World";
vm.Text = "Hallo World 2";
Console.ReadLine();
}
class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _text;
public string Hello
{
get { return "Hello " + Text; }
}
public string Text
{
get
{
return _text;
}
set
{
if(_text != value)
{
_text = value;
OnPropertyChanged();
OnPropertyChanged(nameof(Hello));
}
}
}
void OnPropertyChanged([CallerMemberName] string name = "") // EventInvocator
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment