|
-
Jan 17th, 2009, 01:41 PM
#1
[RESOLVED] [2008] Loading data into a ListView in WPF
Hi Guys,
I'm trying to build an app using WPF and I want to display a load of data from some tab delimited log files in a data grid style. I know there is no DGV control for WPF so after watching a few "forms over data" videos on the MSDN site I figured I should be able to do it using a ListView with DataGrid tags within it.
So after messing about with the XAML code for a bit I managed to create my column headers in my ListView etc and I can load data into it fine if there's just one column... but obviously I need more than one column.
So I thought fine I just create my extra columns and then use ListViewItem and ListViewSubItem classes to create the content for the listview and then add them to it. However, it seems that ListViewSubItem does not even exist in WPF and the ListViewItem doesnt seem to have any way of editing separate column data. For example, if I set ListViewItem.Content = "some text" and then add that to the ListView it just sets "Some text" in all 5 of the columns I have.
All of the examples on MSDN are using data bound listviews and I dont want mine to be data bound I just want to manually load it with items. So how do I go about doing this?
Thanks
Chris
-
Jan 17th, 2009, 07:41 PM
#2
Re: [2008] Loading data into a ListView in WPF
The first point to note is that, if you want to use a DataGridView, you can by adding a Windows Forms Control Host, or whatever it's called, and then stick a DGV inside it. WinForms and WPF apps can both host each other's controls during this "transition phase". That may not be what you want but I thought it was worth pointing out.
I've never used WPF at all so I had no idea what the answer to your question was. As I always do in such situations, I went strauight to the MSDN Library. I opened the documentation for the System.Windows.Controls.ListViewItem and immediately found this:
A ListView typically specifies the content for its ListViewItem controls by setting the ItemsSource property or the Items property.
I then followed the links for the Items and ItemSource properties and, while I haven't tested the theory, it seems fairly obviousl that you either assign an array or collection to the ItemSource property or else add individual objects to the Items property. That was less than a minutes work. Hopefully this illustrates why you should always consult the documentation first. This is doubly the case when you already know what type or member you want to use because then you can use the index to go straight to the relevant information without searching.
-
Jan 18th, 2009, 08:43 AM
#3
Re: [2008] Loading data into a ListView in WPF
Yeah thanks I was aware of the winform hosting feature but I figured it is probably best to try and keep it all "native WPF" if you know what I mean.
As for the ItemSource property, I did see that but I thought it was just for bound controls (I guess I got it confused with BindingSource). However, even after reading the MSDN doc on it I still dont really understand how you can use this to specify data for different columns...
For example, if I do this:
vb Code:
Dim items As New List(Of String)
'Looping through some text file
For Each something In SomeList
items.Add(something)
Next
MyListView.ItemSource = items
Then I end up with the same results, every column has the same text in it. Which isnt surprising really as there's no way it could know from that code what should go where. So how can I make an array/list/collection that I can set as the ItemSource that actually tells it which column to put things in. I dont get it... I'll keep reading though and see what I find
-
Jan 18th, 2009, 08:46 AM
#4
Re: [2008] Loading data into a ListView in WPF
ListViewItem has an indexer, which means that you should be able to use ListViewItem(0) to access the first column, and so on. is that what you meant?
-
Jan 18th, 2009, 08:48 AM
#5
Re: [2008] Loading data into a ListView in WPF
Doesnt seem to let me do that... I just get "This item cannot be indexed as it has no default property"
-
Jan 18th, 2009, 09:02 AM
#6
Re: [2008] Loading data into a ListView in WPF
I found this article: http://www.switchonthecode.com/tutor...istview-part-1
Which is helping a bit... but the fact its in C# is making it a little tricky to use. I've never even heard of an "ObservableCollection" before either so im not really sure what I'm doing but I'll carry on trying to get it working, in the mean time if anyone has any tips/comments I'd be glad to hear them
-
Jan 18th, 2009, 09:09 AM
#7
Re: [2008] Loading data into a ListView in WPF
Sorry, had my C# head on. ListViewItem.Item(0) should've done it.
ObservableCollection is similar to a list in that it inherits from Collection. So it's similar, it just does different things, in this case, lets you know when something gets added to it. What part of it is confusing you?
-
Jan 18th, 2009, 09:23 AM
#8
Re: [2008] Loading data into a ListView in WPF
Nope ListViewItem.Item does not exist. Dont forget this is WPF, not normal winforms Vb.NET. Thats why I'm struggling so much lol
I've got it partly working now, it seems that with WPF when you want to bind a column to something you have to create public properties for pretty much everything that has anything to do with what you are binding. So in my case I have to do this:
vb Code:
Public ReadOnly Property EmailCollection() As ObservableCollection(Of EmailData)
Get
Return _EmailCollection
End Get
End Property
Private _EmailCollection As ObservableCollection(Of EmailData) = New ObservableCollection(Of EmailData)
Public Structure EmailData
Private _FromAddress As String
Private _ID As String
Public Property FromAddress() As String
Get
Return _FromAddress
End Get
Set(ByVal value As String)
_FromAddress = value
End Set
End Property
Public Property ID() As String
Get
Return _ID
End Get
Set(ByVal value As String)
_ID = value
End Set
End Property
End Structure
and then to add items I do this in my loop:
vb Code:
Dim email As New EmailData
email.ID ="something"
email.FromAddress = "something else"
_EmailCollection.Add(email)
And in my XAML I have to define the listview with its ItemSource property set to "EmailCollection" and then each header has to have its BindingDisplayMember property set to the name of the property within my EmailData structure that it is supposed to display.
Maybe I'm just doing something wrong but if you ask me this is way too much work just to load a listview with some items..
Anyway, the code above works in that it sets the data of more than one column but for some reason it is not setting one of the columns correctly. It sets my ID column to the correct integer value but one of the other columns which just holds a string value gets set to "MyProjectName.MyFormName + EmailData"
Any ideas? 
in that it sets more than one column but the problem is that it sets one column to the correct data but sets the other column
-
Jan 18th, 2009, 09:41 AM
#9
Re: [2008] Loading data into a ListView in WPF
OK nevermind that last bit it was just me being stupid. I forgot I had renamed one of the members of my EmailData structure and hadnt updated the XAML to reflect this.
Gosh I can see why this whole "separate the UI from the code" is such a great idea....
I guess I can mark this as resolved now, but I'm sure I will come unstuck as soon as I want to actually do anything with the data in the listview soon enough..
Thanks for the help guys
-
Jan 18th, 2009, 04:39 PM
#10
Re: [RESOLVED] [2008] Loading data into a ListView in WPF
I said the ItemSource of the ITEM, not the CONTROL.
-
Jan 18th, 2009, 04:43 PM
#11
Re: [RESOLVED] [2008] Loading data into a ListView in WPF
the ITEM does not contain such a property... even the quote you pasted from MSDN made it clear it was refering to the CONTROL not the item (at least I thought it did)
A ListView typically specifies the content for its ListViewItem controls by setting the ItemsSource property or the Items property.
Why would ListViewItem have an Items property or an ItemSource property when the class ListViewSubItem no longer exists
EDIT: After re-reading that MSDN quote, I'm a little confused as to what exactly you mean by ITEM as they seem to class a ListViewItem as a control as well (I thought by Control you meant the ListView itself).
Last edited by chris128; Jan 18th, 2009 at 04:50 PM.
-
Jan 18th, 2009, 05:38 PM
#12
Re: [RESOLVED] [2008] Loading data into a ListView in WPF
Ah, b*gger! That's what happens when you try to browse the web on a mobile phone. My apologies for my mistake.
Like I said, I've never used WPF but maybe this is a chance to learn so I'll have a play around myself and see if I can come up with anything else.
-
Jan 18th, 2009, 06:05 PM
#13
Re: [RESOLVED] [2008] Loading data into a ListView in WPF
haha no worries Well I mean I have got it working now (see my earlier post) but feel free to see if you can find a better/easier way of doing it. Although I'm beginning to doubt that there will be as everything in WPF seems to take about 5 times as much time and code than in winforms... You cant even add components to your forms in the designer Only controls.
Cheers
Chris
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|