Selectable read only objects

Selectable read only objects

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


Michael posted on Monday, June 08, 2009

I often need to bind a ROL of ROB to a WinForms grid with the ability to select items via a CheckBoxColumn. The set in the following property never gets hit when the cell is clicked:

bool m_selected;
public bool Selected
{
    get { return m_selected; }
    set
    {
        m_selected = value;
        OnPropertyChanged("Selected");
    }
}


The code does work if the ROB is changed to a BB, or by making it a non-CSLA object that implements INotifyPropertyChanged. Has anyone else tried something like this?

Regards
Michael

sergeyb replied on Tuesday, June 09, 2009

Yes.  You need to implement INotifyProopertyChanged interface inside your ROB.

 

ROBInstance: BB< ROBInstance>, INotifyPropertyChanged

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: Michael [mailto:cslanet@lhotka.net]
Sent: Tuesday, June 09, 2009 12:01 AM
To: Sergey Barskiy
Subject: [CSLA .NET] Selectable read only objects

 

I often need to bind a ROL of ROB to a WinForms grid with the ability to select items via a CheckBoxColumn. The set in the following property never gets hit when the cell is clicked:

bool m_selected;
public bool Selected
{
    get { return m_selected; }
    set
    {
        m_selected = value;
        OnPropertyChanged("Selected");
    }
}


The code does work if the ROB is change to a BB. Has anyone else tried something like this?

Regards
Michael



tetranz replied on Tuesday, June 09, 2009

sergeyb:
Yes.  You need to implement INotifyProopertyChanged interface inside your ROB.

ROBInstance: BB< ROBInstance>, INotifyPropertyChanged

Yeah but I don't think lack of INotifyPropertyChanged explains why Michael's set code never runs. It means that the UI won't automatically know about the change if the property is updated in code. I've done something similar, blurring the definition of read only so I don't know why it's not working. I can bind any plain old object to a grid and the properties get updated (i.e, set runs) when edited in the grid.


Have you tried a simple List<ROB> or even an array? Does the set work then? That would indicate whether or not it's really related to the ROL or something else.

Michael replied on Tuesday, June 09, 2009

ROB already implements INotifyProopertyChanged in Csla.Core.BindableBase (if it didn't the code I posted would not build).

I tried a List<ROB> and that does indeed work, so ROL must be breaking it somehow.

Is this "by design"? (Rocky?) Is there a workaround? I'd obviously prefer to use ROL rather than plain List.

Regards
Michael

SonOfPirate replied on Tuesday, June 09, 2009

Your problem is most likely caused by the fact that the ReadOnlyListBase or, more specifically, the Core.ReadOnlyBindingList class it inherits is marked as read-only be default.  When you bind the object to a UI control, such as a DataGrid, the control looks at the IBindingList interface to determine what behaviors are supported.  This includes the IsReadOnly property (from IList) as well as AllowEdit, AllowNew and AllowRemove.  The latter three are set to false by default.

If you want a control to allow users to toggle your Selected property, you'll need to either override the AllowEdit property or set it to true in your constructor.  I believe you may also need to change IsReadOnly to false, but try changing AllowEdit first to be sure.  You will most likely need to apply the Bindable attribute to your business object so that other properties can't be edited in the UI.

Hope this helps.

 

Michael replied on Tuesday, June 09, 2009

Setting ROLB.AllowEdit = true is all that's required. Thanks for your help.

Michael replied on Thursday, April 26, 2012

I've spent the last few days upgrading our projects from CSLA 3.8 to 4.3. Sadly, all of our selectable ReadOnlyBase / ReadOnlyListBase collections bound to WinForms DataGridViews are broken. When I first asked this question, the problem was in the ROLB; binding to a plain List<T> of ROB behaved correctly. However,  with 4.3 even binding to a plain List does not work. So, that makes me wonder if the problem is in ROB.

When programatically changing the Selected field for all items, only the binding source's current item responds to the OnPropertyChanged (the check box for the current row is updated correctly). No other rows respond. When a different row is clicked, its check box is updated. So, what's different?

I know this is an old thread, so If I don't get any help here I'll start a new one.

Regards
Michael

JonnyBee replied on Thursday, April 26, 2012

For Windows Forms with CSLA 4.3 you must change to use the BindingList derived base class:

ReadOnlyBindingListBase and
BusinessListBindingListBase  etc

The ReadOnlyListBase and BusinessListBase is now derived from ObservableCollection that does not work properly with Windows Forms.

 

Michael replied on Thursday, April 26, 2012

Fantastic, thanks Jonny!

Copyright (c) Marimer LLC