0

I am basically making a listview and have that bound to a wpf listview. Here is a snippit of a piece of my code. I am trying to output the listview to streamwriter, but only know how to do that in a text box. How would I convert the listview to text or a textbox to save to a file with stream writer?

List<House> Inventory = new List<House>();
Inventory.Add(new House() { Price = "105000", Address = "789 Burn Street", Zip = "83619", Bedrooms = "1", Bathrooms = "1", Sqft = "885", Year = "1954", HOA = "450" });
lvInventory.ItemsSource = new List<House>();
DDJ
  • 807
  • 5
  • 13
  • 31
  • 1
    Im assuming you want to write the data bound to the ListView right? In that case its just a regular List so writing to a streamwriter works the same as writing any list regardless of it being bound to a ListView – Gordon Allocman Mar 10 '16 at 21:00
  • Can you show mean what you mean in some snippit of code? Thanks – DDJ Mar 10 '16 at 21:17
  • My code shows :System.Collections.Generic.List`1[ClassAttemptLibrary.House] in the textbox – DDJ Mar 10 '16 at 21:18
  • 1
    I am a little confused about what you want. You say streamwriter so I assume you are trying to write to a file? A ListView is just a display of a list of items. File IO is handled in code and the ui you are using shouldn't matter. From your last comment it sounds like you are having trouble with binding not File IO – Gordon Allocman Mar 10 '16 at 21:29
  • I didn't think of that. Binding in relating to a text box. I'll have to look into that. I appreciate the info. – DDJ Mar 10 '16 at 21:34
  • I am just using a textbox to see my output before it is a text file – DDJ Mar 10 '16 at 21:35
  • 1
    Are you trying to save the object to be read in again? Or are you trying to just write the house information in text form? If it is the former, look into Serialization, specifically the ISerializable interface or the [Serializable]. If it is the latter, your House class needs a ToString method then you can have a for loop through the list writing each House's ToString to a file. – Gordon Allocman Mar 10 '16 at 21:39
  • 1
    The reason you get System.Collections.Generic.List`1[ClassAttemptLibrary.House] in the textbox is because that is the default ToString method of a List object – Gordon Allocman Mar 10 '16 at 21:42
  • So do I have to separate each string within my array of "Inventory" - I am guessing then put it back into the text box. Would I have to call price, then address, etc the do a streamwriter? – DDJ Mar 12 '16 at 04:08
  • 1
    TextBox is bound to a single string. If you want to display it in the TextBox I would suggest choosing a single string property to display. You can do a [multi-binding](http://stackoverflow.com/questions/2552853/how-to-bind-multiple-values-to-a-single-wpf-textblock) that binds the TextBox to all of the properties, but that may be displaying a bit too much information. When writing to the file I would suggest writing a ToString method in House that returns a formatted string of the properties for you to then call and write to a file – Gordon Allocman Mar 14 '16 at 13:01

0 Answers0