0

I'm using a LinkCollection-property of a ViewModel for binding, the LinkCollection is filled in the constructor of the ViewModel:

    public SurveySelectionViewModel()
    {
        foreach (var year in Surveys().Select(x => x.year.ToString()).Distinct())
        {
            this.Years.Add(new Link { DisplayName = year, Source = new Uri("http://www.stackoverflow.com") });
        }
    }

The years are shown in the view when my ListCollection "Years" is defined like this:

private LinkCollection years = new LinkCollection();
public LinkCollection Years
{
    get { return this.years; }
    set
    {
        if (this.years != value)
        {
            this.years = value;
        }
    }
}

Why are the years not showing when I reduce the above to this:

public LinkCollection Years = new LinkCollection();

The LinkCollection is still being filled with years.. but they are not showing.

peter
  • 2,103
  • 7
  • 25
  • 51

1 Answers1

1

Because public LinkCollection Years = new LinkCollection(); is defining Years as a field, not a property, and you cannot bind to fields in WPF.

What you could do is this:

public LinkCollection Years { get; private set; }

then set Years in your constructor:

Years = new LinkCollection();
Steven Rands
  • 5,160
  • 3
  • 27
  • 56