Results 1 to 13 of 13

Thread: [RESOLVED] [2008] Loading data into a ListView in WPF

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Resolved [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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    1. Dim items As New List(Of String)
    2. 'Looping through some text file
    3. For Each something In SomeList
    4.   items.Add(something)
    5. Next
    6. 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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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?

  5. #5

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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"
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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?

  8. #8

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    1. Public ReadOnly Property EmailCollection() As ObservableCollection(Of EmailData)
    2.         Get
    3.             Return _EmailCollection
    4.         End Get
    5.     End Property
    6.  
    7.     Private _EmailCollection As ObservableCollection(Of EmailData) = New ObservableCollection(Of EmailData)
    8.  
    9.     Public Structure EmailData
    10.         Private _FromAddress As String
    11.         Private _ID As String
    12.  
    13.         Public Property FromAddress() As String
    14.             Get
    15.                 Return _FromAddress
    16.             End Get
    17.             Set(ByVal value As String)
    18.                 _FromAddress = value
    19.             End Set
    20.         End Property
    21.  
    22.         Public Property ID() As String
    23.             Get
    24.                 Return _ID
    25.             End Get
    26.             Set(ByVal value As String)
    27.                 _ID = value
    28.             End Set
    29.         End Property
    30.     End Structure

    and then to add items I do this in my loop:
    vb Code:
    1. Dim email As New EmailData
    2. email.ID ="something"
    3. email.FromAddress = "something else"
    4. _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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2008] Loading data into a ListView in WPF

    I said the ItemSource of the ITEM, not the CONTROL.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width