Dynamically add rows too a GridView ListBox in WPF
Hi guys!
I've got a GridView ListBox that is connected, via bindings to an array of other objects and shows various properties of the stored objects in the ListBox.
However I can't seem to figure out a way to manually add an item to the ListBox that would cover all the columns using code. I need to do this for when there are no items in the array.
A blank object cannot be used here so does anyone know how to progrmatically add one?
Re: Dynamically add rows too a GridView ListBox in WPF
There might be a better way but I would just unbind the listbox - set the ItemSource property to Nothing or just to a collection that you declare in code that contains only the single item you want to add. As for making this single object look different to the normal items when the listbox is populated, just change the DataTemplate via code to a DataTemplate that displays the item in the way you want.
Re: Dynamically add rows too a GridView ListBox in WPF
Seems like a lot of work for something that would have been so simple with Windows Forms. Oh well. I think maybe they need to work on this for a later version of WPF.
I will take a look at this and let you know how it goes. I take it then that there are no easy ways to directly add items into the GridView?
Re: Dynamically add rows too a GridView ListBox in WPF
Actually. I don't think the above suggestion will work. Here are some screen shots that may help clarify things. Currently I have this (see attached).
Since there are no matched there is nothing to display in the listbox. It would be nice to have something there so I guess I could add a blank item in there but I'm trying to avoid that.
I was hoping to be able to add one spanning item in there that would cover all three columns but I take it that's not really an option here?
Re: Dynamically add rows too a GridView ListBox in WPF
Ohhhh right you just want to manually add an item if your search doesnt return any results? I assume then that you are binding your listview to a collection of some sort - so why cant you just add a single item to that collection? To me that is no more work than winforms, its different admittedly but not harder. You are just adding an item to a collection in code rather than adding an item directly to the listview
Re: Dynamically add rows too a GridView ListBox in WPF
There is a reason. Each of them is a member of a class that basically does not allow certain values to be empty. And even if it did allow empty results, that would still display items in the columns not quite in the way I would like.
I may have found another solution though. Hide the view in an expander that is automatically expanded when there are results. I will probably add a text element above the list view for more information but I think the fault here is me trying to use the ListView for something other than it was intended for
Re: Dynamically add rows too a GridView ListBox in WPF
Here is an extra question then. Is it possible to define a style type (or something along those lines) and then apply the style (or whatever) to the GridView dynamically in order to switch the columns.
So. If I have two sets of columns, two styles with different names, switching with switch the columns shown. Is there a way to do this?