Adding a checked property to a read-only collection

Adding a checked property to a read-only collection

Old forum URL: forums.lhotka.net/forums/t/1428.aspx


JonM posted on Monday, October 09, 2006

Say I need to add a checkbox to several of my read-only collection.  I don't need to save the property or anything.  Please tell me I don't have to convert my read-only collections into standard businessCollections.  Any ideas would be great.

RockfordLhotka replied on Tuesday, October 10, 2006

You just want to have a checkbox on each item, but not persist that value?

You should be able to have a read-write property on your child class then. Obviously you can't persist it, but you could load the initial values from the database, and when you bind the object it will be read-write to the UI.

The only complication is if you need to change the property programmatically (i.e. not directly through the UI via data binding) then you'll need to raise a PropertyChanged event in that property's set block. To do this you need to implement INotifyPropertyChanged like I do in BindableBase - but also make sure to mark both delegate fields as NotUndoable! Obviously you'd implement this interface directly in your child object class.

JonM replied on Tuesday, October 10, 2006

I've tried that but it does not seem to work. If I change the value of the row in the DataGridView programatically it updates my business object.  For some reason, clicking on the checkbox itself doesn't do anything.  The Set method of my property isn't being called.  I've verified that the grid is NOT read-only.  I'm a little stumped.  Now if I don't bind the checkbox column in the datagridview to the business object property, I can change the checkbox.  I may have to make due with this.. and check the value of the checkbox in the grid instead of in the object.

rfcdejong replied on Wednesday, November 04, 2009

Ok, an very old post, but we stumbled into the same problem.
Having readonly lists we do want a checkbox binded to "IsChecked" and it should just be on the business object but not persistable. And we want to reload a list when IsChecked is changed, but that can be done.

But for some reason the Grid just doesn't want to go in editmode, even when the ROLB has IsReadOnly = false.

I'm almost going to refactor al my ROLB to BLB.

Is there a solution?

rfcdejong replied on Wednesday, November 04, 2009

Nevermind, just found the solution.

I was looking at IsReadOnly = false but that is the wrong property, looking at the ReadOnlyBindingList it is inherited from ExtendedBindingList so that would not be it. But in the constructor the property AllowEdit is set to false

So after getting the list just set AllowEdit = true;

PS: Perhaps setting IsReadOnly should set AllowEdit?

RockfordLhotka replied on Wednesday, November 04, 2009

The intent of ReadOnlyListBase is to create read-only lists. If people want to misuse it for other purposes that's fine, but I feel no need to enable bad behavior :)

Copyright (c) Marimer LLC