James Armstrong James Armstrong - 1 year ago 114
C# Question

Caliburn.Micro - Calling method guard when updating field of item in BindableCollection

I have a property

BindableCollection<Cat> { get; private set; }
with a number of
objects that is bound to an ItemsControl in my WPF XAML.

I then have the following code that is attached to a button inside this ItemsControl.

XAML snippet

<Button Content="Pat Cat" cal:Message.Attach="PatCat($dataContext)" />

In the ViewModel...

BindableCollection<Cat> { get; private set; }

public bool CanPatCat(Cat cat)
return cat.Pattable && cat.Alive;

public void PatCat(Cat cat)
// pat the cat
cat.Alive = false;

Given this is a method guard and not a property, how do I notify the UI so that the CanPat method is evaluated for each Cat in the BindableCollection given the guard method will now return false once the Cat has been pat?

Answer Source

I ended up creating a separate ViewModel for each Cat, so a CatViewModel, with a corresponding CatView. I was then able to notify that the bindable collection had changed, and things worked as expected.

Note, I had incorrectly placed ViewModel logic into my Cat model. These were the properties that were changing on my model. I moved these into the CatViewModel.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download