For a large fillin form I use the asp.net FormView for the magic databinding to my model. In this model I've a property called Rides (shown below), wh开发者_运维百科ich exposes a list, which I obviously not want to be replaced entirely. So I made it readonly.
However, this way I can't use the databinding features anymore. Is there a common solution for this problem?
public IList<Ride> Rides
{
get
{
if (this._rides == null)
{
this._rides = new List<Ride>();
}
return this._rides;
}
}
You can still use the databinding, instead of <%# Bind("PropertyName")%> use <%# Eval("PropertyName")%>, as Bind is usually for 2 way binding & Eval is usually for evaluation of the underlying datasources, Bind will be handy when you have a setter as well. Hope this sheds enough light & you will find it helpful.
Monty, Take a look at a class named BindingList. Binding list enables two-way binding. You can create it from Yor collection in code and bidn it to the datasource property of the FormView. I think this is what You want. Also by exposing IList YOU have not made this thing read-only. I can recast to the List and I can modify You items all the way. If You really want to expose rides as read-only return IEnumerable and return not a List by a ReadOnlyCollection... recasting list to the other class wont' help.
Wild stab in the dark, don't know if this will work but I guess you can try...
How about putting the setter (set) back in the property, but make it assign to itself?
eg.
set
{
this._rides = this._rides; // Rather than this._rides = value;
}
This would mean the value is untouchable from the property, if a set operation is attempted it won't do any damage and should still bind...
精彩评论