|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
Adding NothingDear List,
Sometimes I need to show a dropdown with a list of options (being Entity objects) and a possibility to choose a "null" option. If it's a search filter, I need something like "show all", if it's a property when editing an object, I need something like "unknown" (to insert a null in the database). An obvious solution would be to insert a null into the collection I'm binding to, and then somehow make the dropdown show a desired string instead. However, the combo gets really ugly in that case -- the textbox part won't get painted, and the list part won't display the "nothing" line. Perhaps I could construct a dummy object and use it somehow, but with Neo's strongly typed collections I should construct a dummy object for every type I have. Or can I cast it to Ilist and add it this way (as an Object, to avoid type checking)? Also, how do I create a dummy object without a context? Any experience with such things? Should be a pretty common situation, hasn't anybody a nice solution? No GoF pattern for this? Thanks ulu --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: Adding NothingWe had exactly this problem on the project Neo was created for. My memory is a bit hazy but the solution we used at the time involved: - a subclass of ObjectContext with no fields, methods or properties public class NullContext : ObjectContext - a "null entity object" of each required entity from the NullContext NULL_PRODUCT = new ProductFactory(new NullContext()).CreateObject (); NULL_PRODUCT.name = ""; - a strongly typed collection of all options with the null object added for the popup ProductList products = productFactory(context).findAllObjects(); products.Add(Product.NULL_PRODUCT); - an override of SetRelatedObject in our custom entity object base class protected override void SetRelatedObject(IEntityObject eo, ...) { if((eo == null) || (eo.Context is NullContext)) { Row[keyColumnInThisEntity] = DBNull.Value; return; } Hope this helps. If you get it to work, what about writing up a recipe on the Neo Wiki...? erik On 25 Aug 2006, at 13:57, Ulu wrote: > Dear List, > > Sometimes I need to show a dropdown with a list of options (being > Entity objects) and a possibility to choose a "null" option. If > it's a search filter, I need something like "show all", if it's a > property when editing an object, I need something like > "unknown" (to insert a null in the database). > > An obvious solution would be to insert a null into the collection > I'm binding to, and then somehow make the dropdown show a desired > string instead. However, the combo gets really ugly in that case -- > the textbox part won't get painted, and the list part won't display > the "nothing" line. > > Perhaps I could construct a dummy object and use it somehow, but > with Neo's strongly typed collections I should construct a dummy > object for every type I have. Or can I cast it to Ilist and add it > this way (as an Object, to avoid type checking)? Also, how do I > create a dummy object without a context? > > Any experience with such things? Should be a pretty common > situation, hasn't anybody a nice solution? No GoF pattern for this? > > Thanks > > ulu > > > > --------------------------------------------------------------------- > To unsubscribe from this list please visit: > > http://xircles.codehaus.org/manage_email > --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
|
Re[2]: Adding NothingHi List,
Monday, August 28, 2006, 6:31:02 PM, I'm reading this: ED> We had exactly this problem on the project Neo was created for. My ED> memory is a bit hazy but the solution we used at the time involved: ED> - a subclass of ObjectContext with no fields, methods or properties ED> public class NullContext : ObjectContext ED> - a "null entity object" of each required entity from the NullContext ED> NULL_PRODUCT = new ProductFactory(new NullContext()).CreateObject ED> (); ED> NULL_PRODUCT.name = ""; ED> - a strongly typed collection of all options with the null object ED> added for the popup ED> ProductList products = productFactory(context).findAllObjects(); ED> products.Add(Product.NULL_PRODUCT); ED> - an override of SetRelatedObject in our custom entity object base class ED> protected override void SetRelatedObject(IEntityObject eo, ...) ED> { ED> if((eo == null) || (eo.Context is NullContext)) ED> { ED> Row[keyColumnInThisEntity] = DBNull.Value; ED> return; ED> } ED> Hope this helps. If you get it to work, what about writing up a ED> recipe on the Neo Wiki...? ED> erik I've successfully tried a simpler (I believe) version. 1. Create a DummyObject that implements IEntityObject (as we can only add such objects to an ObjectList) add a 1-parameter constructor: public DummyObject (string StringToShow); The object has to override ToString() to return the string we've provided (StringToShow), since this is a universal way to display this object in a combo; 2. Add it to your collection: ((IList)myCategoryList).Insert(0,new DummyObject("Not set")); (I'm casting my list of categories to an Ilist so that I can insert an IEntityObject). 3. After I bind it to a combo, I see the first item is a string "Not set". Here I'm not setting "DisplayMember" nor "ValueMember" parameters of my combo, letting my objects display themselves via the overridden ToString(). I'll be using SelectedItem (rather than SelectedValue), so I'm binding it to the Category property of my Article object. Now, what happens if somebody chooses this dummy value? We've bound the SelectedItem to the "Category" property, so it accepts only Categories. If the property isn't nullable, it's perfect -- the combo itself won't allow us to choose this value. If it *is* nullable, we have to convert our dummy to null. We can use a SelectedIndexChanged event, for example. Check the SelectedItem property for its type (or if its Context is null), and set SelectedItem=null. Why do I like it this way? I don't have to create a new context, a new nullproduct for every entity type, and override entity methods. Perhaps you are using databinding in a different way, but I've found it the most convenient way for me anyway. But what's the best about it, I'm not changing my model layer because of the way I want to present it. In other words, all the stuff related to presentation happens in the presentation layer. The next good thing that could happen to this idea is to subclass the combo itself and put all these things into it. I'll put it to the Wiki after I test it a little. Ulu. ED> On 25 Aug 2006, at 13:57, Ulu wrote: >> Dear List, >> >> Sometimes I need to show a dropdown with a list of options (being >> Entity objects) and a possibility to choose a "null" option. If >> it's a search filter, I need something like "show all", if it's a >> property when editing an object, I need something like >> "unknown" (to insert a null in the database). >> >> An obvious solution would be to insert a null into the collection >> I'm binding to, and then somehow make the dropdown show a desired >> string instead. However, the combo gets really ugly in that case -- >> the textbox part won't get painted, and the list part won't display >> the "nothing" line. >> >> Perhaps I could construct a dummy object and use it somehow, but >> with Neo's strongly typed collections I should construct a dummy >> object for every type I have. Or can I cast it to Ilist and add it >> this way (as an Object, to avoid type checking)? Also, how do I >> create a dummy object without a context? >> >> Any experience with such things? Should be a pretty common >> situation, hasn't anybody a nice solution? No GoF pattern for this? >> >> Thanks >> >> ulu >> >> >> >> --------------------------------------------------------------------- >> To unsubscribe from this list please visit: >> >> http://xircles.codehaus.org/manage_email >> ED> --------------------------------------------------------------------- ED> To unsubscribe from this list please visit: ED> http://xircles.codehaus.org/manage_email ulu --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
| Free embeddable forum powered by Nabble | Forum Help |