0

I've spent a good couple of days failing at solving this issue myself, which leads me to believe I don't understand something very basic about xaml and the code-behind. I am attempting to display data from an ObservableCollection in a ListView using WPF and c#.

When I start debugging, the window displays the listview, with the grid and the headers. There are clickable rows equivalent to the number of items that should be there, but the items appear blank or empty. I was working off an incomplete tutorial to try and learn my way through this, the code is as follows:

MainWindow.xaml.cs:

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

NameList.cs:

class NameList : ObservableCollection<PersonName>
{
    public NameList()
        : base()
    {
        Add(new PersonName("Willa", "Cather")); //enumerated? (firstName, lastName)
        Add(new PersonName("Isak", "Dinesen"));
        Add(new PersonName("Victor", "Hugo"));
        Add(new PersonName("Jules", "Verne"));
    }
}

PersonName.cs:

class PersonName
{
    private string firstname;
    private string lastname;

    public PersonName(string first, string last)
    {
        this.firstname = first;
        this.lastname = last;
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
}

I would include the xaml, but I haven't figured out how to do that in a question here, so I'll just spell it out as best I can:

ListView ItemsSource=""{Binding Source={StaticResource NameListData}}"

GridViewColumn DisplayMemberBinding="{Binding Path=FirstName}"

GridViewColumn DisplayMemberBinding="{Binding Path=LastName}"

I've only listed the actual bindings, I didn't think the layout info was too important, but let me know if I'm wrong in that assumption.

6
  • You are probably getting binding error messages in your output window in Visual Studio, assuming you are using VS. What do they say? Commented Feb 8, 2013 at 20:30
  • Where are you instantiating NameListData? Commented Feb 8, 2013 at 20:32
  • @Phoenix, I'm not getting any errors in my output window. Everything compiles without complaint, it just displays empty rows. Commented Feb 8, 2013 at 20:36
  • You would see the binding errors at runtime. Commented Feb 8, 2013 at 20:37
  • @Mash, NameListData is first listed in the xaml under Window.Resources as c:NameList x:Key="NameListData" Commented Feb 8, 2013 at 20:38

3 Answers 3

5

You assign a value to private field

this.firstname

but bind to property

public string FirstName { get; set; }

This should work

public PersonName(string first, string last)
{
    this.Firstname = first;
    this.Lastname = last;
}
Sign up to request clarification or add additional context in comments.

2 Comments

your answer threw some errors, but it set me down the right path. Thanks for the inspiration!
@spugm1r3 You should have marked it as 'Answer' if you thought it took you in the right direction...
2

Your MainWindow will need a property with your data so that you can bind to

public partial class MainWindow : Window
{
    private NameList _nameList;

    public NameList NameList
    {
        get { return _nameList; }
        set
        {
            if (_nameList != value)
            {
                _nameList = value;
            }
         }
    }

    public MainWindow ()
    {
        InitializeComponent(); 

        NameList = new NameList();
    }
}

Your PersonName class needs to assign the properties (not the members) so that you can bind to them:

public class PersonName
{
    public PersonName(string first, string last)
    {
        FirstName = first;
        LastName = last;
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
}

And in your XAML you can bind like this:

<ListView ItemsSource="{Binding NameList}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <Run Text="{Binding LastName}" /><Run Text=", " /><Run Text="{Binding FirstName}" />
            </TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

So your result will look like this:

enter image description here

3 Comments

What was so frustrating is that I've spent so much time trying to understand databinding, that I was dead certain that my problem lay in the xaml. Sometimes you just can't rely on errors and exceptions to you something is wrong with your code.
It's always the thing you last expect ;)
you know how dumb I feel right now? I spent last 3 hours or so wondering why my darn listview was blank... duhhhhhh cause I didn't have get and set...!!! thank you!
1

So I just figured it out, a flash of understanding sparked by @LPL.

When I declared my property, I didn't set the get and set values to the private strings, so when the binding called for them, it knew that four items existed, but they were empty items. The now functional code looks like this:

 public string FirstName
    {
        get {return firstname; }
        set{ firstname=value; }
    }

With an equivalent property for LastName. Joy to simple problems becoming massive headaches.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.