1

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!

1
  • Any binding error in the output window? Tried changing Tally to a property rather than a field? Commented Dec 2, 2011 at 21:52

2 Answers 2

4

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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.

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.