Skip to content

Instantly share code, notes, and snippets.

@runceel
Created March 10, 2015 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save runceel/c7a361e8d5164bb0dff9 to your computer and use it in GitHub Desktop.
Save runceel/c7a361e8d5164bb0dff9 to your computer and use it in GitHub Desktop.
using Microsoft.Practices.Prism.Mvvm;
using Reactive.Bindings;
using Reactive.Bindings.Extensions;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Linq;
using System.Threading;
namespace RxPropEdu
{
class Program
{
static void Main(string[] args)
{
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
var deleted = new ObservableCollection<Person>();
var c = new ObservableCollection<Person>(
Enumerable.Range(1, 10).Select(x => new Person { Name = "tanaka" + x }));
c.ObserveElementProperty(x => x.IsDelete)
.Subscribe(x =>
{
if (x.Value)
{
deleted.Add(x.Instance);
}
else
{
deleted.Remove(x.Instance);
}
});
c.ObserveAddChangedItems()
.Merge(c.ObserveReplaceChangedItems().Select(xs => xs.NewItem))
.Subscribe(xs =>
{
foreach (var p in xs)
{
if (p.IsDelete) { deleted.Add(p); }
}
});
c.ObserveRemoveChangedItems()
.Merge(c.ObserveReplaceChangedItems().Select(xs => xs.OldItem))
.Subscribe(xs =>
{
foreach (var x in xs) { deleted.Remove(x); }
});
var isDeleted = deleted.CollectionChangedAsObservable()
.Select(_ => deleted.Count != 0)
.ToReactiveProperty();
Console.WriteLine(isDeleted);
c[0].IsDelete = true;
Console.WriteLine(isDeleted);
c.RemoveAt(0);
Console.WriteLine(isDeleted);
}
}
class Person : BindableBase
{
private string name;
public string Name
{
get { return this.name; }
set { this.SetProperty(ref this.name, value); }
}
private bool isDelete;
public bool IsDelete
{
get { return this.isDelete; }
set { this.SetProperty(ref this.isDelete, value); }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment