Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView with TreeViewItems in xaml

Tags:

c#

wpf

c#-4.0

xaml

I'm pretty new to c#, the first thing that I'm trying to make is a ListView with data bindings which has turned out ok.

I'm now trying to make items have a twist button if the underlying model has any children (like the TreeView). Each of the children will have columns the same as all the top level items.

How would I go about doing this? Is there an already existing control like this? If not would I be better off dressing up a TreeView to look like a ListView, or dress up a ListView to look like a TreeView?

I went down the road outlined in this solution which dresses up a TreeView, but the end result looks pretty awful and the heading is actually just an item, so you lose all the nice column sizing and column buttons that can hook up to column sorting that you get in ListView so that route actually seems like it would be more work.

I noticed the new task manager has a control exactly like what I'm trying to create, I don't know how this made? probably in C though.

ListView with TreeView items

like image 754
GP89 Avatar asked Jan 21 '13 17:01

GP89


2 Answers

Microsoft provides a sample that appears to be what you are looking for. A write-up of the example can be found here:

http://msdn.microsoft.com/en-us/library/vstudio/ms771523(v=vs.90).aspx

When you build and run the example you will end up with something resembling this:

enter image description here

There is a large amount of templating done in the example, so you will be able to make things look the way you want.

like image 104
Nicholas Pappas Avatar answered Oct 23 '22 21:10

Nicholas Pappas


What you describe sounds a bit like a TreeListView, and if you google 'WPF TreeListView' you will see some solutions that might be good for you. I have used one from Telerik, but it might be overkill depending on how complicated your needs are.

If you only want one sub-level like the image you attached, you might want to just roll your own using a ListView with a complex DataTemplate for the first column which would show an expander button and a simple ListBox bound to the children items.

Similar to the answer here, except your cell would have a checkbox styled to look like the arrow, the text for the item, and a child ListBox. Then bind the visibility of the child ListBox to the state of the checkbox.

like image 40
Paul Hoenecke Avatar answered Oct 23 '22 19:10

Paul Hoenecke