Villager
Villager

Reputation: 6689

Binding to an Array in WPF

I am working on a WPF application. For some reason, the values in my view model are not showing

MyViewModel.cs

public class MyViewModel : ViewModel
{
  private ObservableCollection<Item> items = Item.GetAll();
  public ObservableCollection<Item> Items
  {
    get { return items; }
    set { items = value; }
  }
}

public class Item
{
  public int ID { get; set; }

  public List<int> Tally = new List<int>();

  public int Total { get; set; }

  public Item(int id)
  {
    this.ID = id;
    for (int i = 0; i < 7; i++)
      this.Tally.Add(0);
  }

  public static ObservableCollection<Item> GetAll()
  {
    ObservableCollection<Item> items = new ObservableCollection<Item>();
    for (int i = 0; i <= 10; i++)
    {
      items.Add(new Item(i));
    }
    return items;
  }
}

MyPage.xaml

<telerik:RadGridView x:Name="myGridView" Grid.Row="1" AutoGenerateColumns="False" 
  ItemsSource="{Binding Path=Items}">
  <telerik:RadGridView.Columns>
    <telerik:GridViewDataColumn Header="ID" DataMemberBinding="{Binding Path=ID}" IsReadOnly="True" />
    <telerik:GridViewDataColumn Header="Monday" DataMemberBinding="{Binding Path=Tally[0], Mode=OneWay}" IsReadOnly="True" Width="1*" />
    <telerik:GridViewDataColumn Header="Tuesday" DataMemberBinding="{Binding Path=Tally[1], Mode=OneWay}" IsReadOnly="True" Width="1*" />
    <telerik:GridViewDataColumn Header="Wednesday" DataMemberBinding="{Binding Path=Tally[2], Mode=OneWay}" IsReadOnly="True" Width="1*" />
    <telerik:GridViewDataColumn Header="Thursday" DataMemberBinding="{Binding Path=Tally[3], Mode=OneWay}" IsReadOnly="True" Width="1*" />
    <telerik:GridViewDataColumn Header="Friday" DataMemberBinding="{Binding Path=Tally[4], Mode=OneWay}" IsReadOnly="True" Width="1*" />
    <telerik:GridViewDataColumn Header="Total" DataMemberBinding="{Binding Path=Total, Mode=OneWay}" />
  </telerik:RadGridView.Columns>
</telerik:RadGridView>

An entry for each Item appears in the grid as expected. However, only the first and last columns have values. The values in the Monday-Friday columns (the columns that reference the Tally List) are not appearing. I'm not sure what I'm doing wrong. Can somebody please tell me?

Thank you!

Upvotes: 1

Views: 6350

Answers (2)

Mark W
Mark W

Reputation: 3909

ou're not using the Tally items anywhere in the observable collections either the local collection you return in the static method or the observable collection in the viewmodel.

Item should just be a Domain Type object(with a few auto getter and setter properties). You should create a service and call it ItemsRepository that would return IEnumerable< Item > collection when you call GetAll() on the repository. Always try to avoid using anything Static if you can, especially a method that would return a collection.

Upvotes: 0

brunnerh
brunnerh

Reputation: 184296

Tally is a field, needs to be a public property.

(I also hope that you are aware of INPC if you want to make runtime changes)

Upvotes: 4

Related Questions