Observable on ReactiveCollection count change

146 views
Skip to first unread message

Sotirios Mantziaris

unread,
Dec 28, 2011, 10:31:25 AM12/28/11
to reacti...@googlegroups.com
Hi,

i want to create a command that is enabled only when a collection contains any items.

public ReactiveCollection<PositionViewModel> NewPositions { get; protected set; }

and the command would be:

public ReactiveCommand ClearPositionCommand { get; private set; }

following the examples i should do something like this:

var canClearPosition = this.WhenAny(x => x.NewPositions, pass => (pass.Value.Count > 0));

ClearPositionCommand = new ReactiveCommand(canClearPosition);

but this doesnt seem to work.

Any suggestions?


Lukáš Čenovský

unread,
Dec 28, 2011, 4:47:16 PM12/28/11
to reacti...@googlegroups.com
It should be:

ClearPositionCommand = new
ReactiveCommand(NewPositions.ColectionCountChanged);

WhenAny watches NewPositions change - e.g. when you assign different
collection to it.

--
-- Lukáš

Sotirios Mantziaris

unread,
Dec 28, 2011, 5:18:32 PM12/28/11
to reacti...@googlegroups.com
ok,

but NewPositions.ColectionCountChanged is a Observable<int> and not Observable<bool> which is required in the constructor.

what i actualy need is a Observable<bool> that return true if the NewPositions has Count != 0 else false.

Any suggestions?

Paul Betts

unread,
Dec 28, 2011, 5:32:43 PM12/28/11
to reacti...@googlegroups.com
How about

Clear = new ReactiveCommand(NewPositions.ColectionCountChanged.Select(x
=> x > 0));

--
Paul Betts <pa...@paulbetts.org>

Lukáš Čenovský

unread,
Dec 28, 2011, 5:33:11 PM12/28/11
to reacti...@googlegroups.com
What about this minor update? ;-)

NewPositions.ColectionCountChanged.Value().Select(cnt => cnt > 0)

--
-- Lukáš

Paul Betts

unread,
Dec 28, 2011, 5:35:54 PM12/28/11
to reacti...@googlegroups.com
CollectionCountChanged's type is just IO<int>, you don't need the Value()

2011/12/28 Lukáš Čenovský <lcen...@gmail.com>:

Lukáš Čenovský

unread,
Dec 28, 2011, 5:38:35 PM12/28/11
to reacti...@googlegroups.com
Oh yes - that happens when you write it from mind instead of testing it :-)

Sotirios Mantziaris

unread,
Dec 28, 2011, 5:38:58 PM12/28/11
to reacti...@googlegroups.com
Paul, that is the reason i didnt find the Value(). 
Your solution did not work either! Am i something missing?

ClearPositionCommand = new ReactiveCommand(NewPositions.CollectionCountChanged.Select(x=>x>0));
ClearPositionCommand.Subscribe(o=>{ClearPosition();}, OnError, OnCompleted);

Paul Betts

unread,
Dec 28, 2011, 5:42:26 PM12/28/11
to reacti...@googlegroups.com
This should definitely work. Are you overwriting NewPositions
somewhere? i.e. somewhere other than the constructor, writing:

NewPositions = new ReactiveCollection<Foo>();

Another thing you probably need to do is:

NewPositions.CollectionCountChanged.StartWith(0).Select(x => x > 0)

--
Paul Betts <pa...@paulbetts.org>

Lukáš Čenovský

unread,
Dec 28, 2011, 5:44:24 PM12/28/11
to reacti...@googlegroups.com
Mind that calling NewPositions.Clear() does not trigger count change. It
is unfortunate but according to spec....

Sotirios Mantziaris

unread,
Dec 28, 2011, 5:47:08 PM12/28/11
to reacti...@googlegroups.com
Paul the StartsWith(0) worked and the command is now ok. thx

Lukas i saw that Clear didnt work. What is the alternative?

Sotirios Mantziaris

unread,
Dec 28, 2011, 5:52:45 PM12/28/11
to reacti...@googlegroups.com
lukas , a straightforward for loop did the job!!!

Thanks for the help!

Sotirios Mantziaris

unread,
Dec 28, 2011, 5:54:35 PM12/28/11
to reacti...@googlegroups.com
Paul thx for the help, really like it once you can get your head around the idea of getting the data pushed to you!!

I am following with great joy!
Reply all
Reply to author
Forward
0 new messages