0

I'm to write out a text file in a ListBox, but all I'm getting is a list "BabyName". My code looks like this:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public void FirstDecadeTopNames_Loaded(object sender, RoutedEventArgs e)
    {
        FileStream fs = new FileStream(@"D:\Dokumenter\Skole\6. semester\GUI\Exercises\Exercise4\WpfApplication1\04-babynames.txt", FileMode.Open);
        StreamReader sr = new StreamReader(fs, Encoding.Default);
        List<BabyName> babyNames = new List<BabyName>();

        while (!sr.EndOfStream)
        {
            BabyName name = new BabyName(sr.ReadLine());
            babyNames.Add(name);
            FirstDecadeTopNames.Items.Add(name);
        }
    }
}

I assume I'm not putting the StreamReader into my list, but I can't see where I'm doing it wrong.

Update:

Here's the XAML for the ListBox:

<ListBox Name="FirstDecadeTopNames"
         Margin="10"
         Loaded="FirstDecadeTopNames_Loaded" >
</ListBox>
Abbas
  • 14,186
  • 6
  • 41
  • 72
Left4Cookies
  • 567
  • 2
  • 7
  • 19
  • Can you show your XAML? Did you bind the properties properly? Because `BabyName` is a class and not a single value, it won't show. Here's an [answer](http://stackoverflow.com/questions/9391746/how-can-i-data-bind-a-list-of-strings-to-a-listbox-in-wpf-wp7/9391815#9391815) of mine that shows how to achieve this. – Abbas Feb 26 '14 at 09:04
  • Hmm... what is the name of your ListBox? There is no code that adds listbox items to your listbox, you only create a list of your BabyName objects. – Rob Feb 26 '14 at 09:06
  • @Robert: this line does it: `FirstDecadeTopNames.Items.Add(name);`. – Abbas Feb 26 '14 at 09:07
  • @Abbas Oh yes of course, here's the XAML I have so far for my ListBox: `` – Left4Cookies Feb 26 '14 at 16:01

3 Answers3

1

I think you shoud use DisplayMemberPath property with your listBox like this:

<ListBox DisplayMemberPath="FieldName"...>
AskT
  • 66
  • 1
  • 2
  • 8
0

You don't show us the code of where the generic list Babyname is declared , however I don't think you need do use it anyway, instead:

List <string> names=new List<string>();

public void FirstDecadeTopNames_Loaded(object sender, RoutedEventArgs e)
{
    FileStream fs = new FileStream(@"D:\Dokumenter\Skole\6. semester\GUI\Exercises\Exercise4\WpfApplication1\04-babynames.txt", FileMode.Open);
    StreamReader sr = new StreamReader(fs, Encoding.Default);

    while (!sr.EndOfStream)
    {
        names.Add(sr.ReadLine());           
    }
    FirstDecadeTopNames.ItemSource=names;
}
Abbas
  • 14,186
  • 6
  • 41
  • 72
apomene
  • 14,282
  • 9
  • 46
  • 72
  • When trying this out, it says the following in the while: type 'string' is not assignable to parameter type 'BabyName'? – Left4Cookies Feb 26 '14 at 16:38
  • And when using your solution it just outputs a blank list? There's a scrollbar there so I assume it's outputting all the rows of the list, but there's nothing in it. – Left4Cookies Feb 26 '14 at 16:45
  • I managed to fix it! It was in my XAML file the problem seemed to be. I just had to add a `Loaded="FirstDecadeTopNames_Loaded"` Thanks for your help! – Left4Cookies Feb 26 '14 at 17:02
0

When you use Binding in WPF without proper path it will Display .ToString() of whatever you are binding. In your case it is a list<BabyName> so it displays only BabyName but not the value.

For example if you have

Class BabyName
{
  public string Name{get;set;}
}

Then bind using {Binding Path="Name"}

Carbine
  • 7,849
  • 4
  • 30
  • 54