The killer question
-
Ed.Poore wrote:
Can the dreaded nullable combo-box be created in WPF.
Ed, Dumb question. What is the dreaded nullable combo-box?
Cheers, Karl
» CodeProject 2008 MVP My Blog | Mole's Home Page | How To Create Screen Capture Videos For Your ArticlesJust a grain of sand on the worlds beaches.
-
Ed.Poore wrote:
Can the dreaded nullable combo-box be created in WPF.
Ed, Dumb question. What is the dreaded nullable combo-box?
Cheers, Karl
» CodeProject 2008 MVP My Blog | Mole's Home Page | How To Create Screen Capture Videos For Your ArticlesJust a grain of sand on the worlds beaches.
Not a dumb question although a quick Google would have given you the idea. Imagine any field where you want to select an item for a relationship in a drop-down or nothing. E.g. you're assigning a bug to a user in your database. You use LINQ or whatever to pull the users from the database, and these are bound to the combobox so you get a list of all the users. But it should also have
(None)
or something similar so that if you select that item then the relationship is set to null. There's been no easy workaround this problem in WinForms so hopefully the magic that is WPF would be able to do it :-\
-
Not a dumb question although a quick Google would have given you the idea. Imagine any field where you want to select an item for a relationship in a drop-down or nothing. E.g. you're assigning a bug to a user in your database. You use LINQ or whatever to pull the users from the database, and these are bound to the combobox so you get a list of all the users. But it should also have
(None)
or something similar so that if you select that item then the relationship is set to null. There's been no easy workaround this problem in WinForms so hopefully the magic that is WPF would be able to do it :-\
You can add the extra row in the data in your business layer or data layer, or simply extend the combobox to function like the ASP.NET dropdown control that has this built in.
Cheers, Karl
» CodeProject 2008 MVP My Blog | Mole's Home Page | How To Create Screen Capture Videos For Your ArticlesJust a grain of sand on the worlds beaches.
-
You can add the extra row in the data in your business layer or data layer, or simply extend the combobox to function like the ASP.NET dropdown control that has this built in.
Cheers, Karl
» CodeProject 2008 MVP My Blog | Mole's Home Page | How To Create Screen Capture Videos For Your ArticlesJust a grain of sand on the worlds beaches.
Karl Shifflett wrote:
You can add the extra row in the data in your business layer or data layer
That's those hacks I mentioned. Not really a good design.
Karl Shifflett wrote:
extend the combobox to function like the ASP.NET dropdown control that has this built in
I wanted to see some code ;P
-
Karl Shifflett wrote:
You can add the extra row in the data in your business layer or data layer
That's those hacks I mentioned. Not really a good design.
Karl Shifflett wrote:
extend the combobox to function like the ASP.NET dropdown control that has this built in
I wanted to see some code ;P
2 thoughts. 1 - do this in exactly the way that Karl suggested and create your own control, or 2 - use an Extension method that would look something like this:
public static void NullableDataSource<T>(this ComboBox source, IList<T> binding)
where T : class, new()
{
if (binding != null)
{
binding.Insert(0, "<Null>");
}
source.DataSource = binding;
}I've not tried this out, I'm just typing the code off the top of my head, but there's no reason that this (or something like this) wouldn't work.
Deja View - the feeling that you've seen this post before.
-
2 thoughts. 1 - do this in exactly the way that Karl suggested and create your own control, or 2 - use an Extension method that would look something like this:
public static void NullableDataSource<T>(this ComboBox source, IList<T> binding)
where T : class, new()
{
if (binding != null)
{
binding.Insert(0, "<Null>");
}
source.DataSource = binding;
}I've not tried this out, I'm just typing the code off the top of my head, but there's no reason that this (or something like this) wouldn't work.
Deja View - the feeling that you've seen this post before.
It does work, on the surface. However if you try and access the lists through other methods then it messes up (i.e. through indicies). I was thinking that there'd be an ice elegant way to create a combo box in WPF which allows it to be set as nullable, rather than having to modify the underlying data. Perhaps creating a new items template thingy and inserting a control there which if selected is null. I.e. the underlying datasource is not modified at all. I'll see what I can do when I get the chance. Just wondering if someone had come up with a solution already.