开发者

Adding additional properties in ViewModel

I have a Silverlight 4 app using EF & WCF RIA Services with a SQL DB. I have a Tasks table that I want to display in a grid or listbox and I want to do a custom grouping. The custom grouping would be Overdue, today, tomrrow, next 7 days and future.

If I understand the concepts of MVVM correctly, I should be creating a custom property for my Tasks object in the TasksViewModel. But I am not sure how to do this.

I have the Tasks entity that is automatically created in the entity data model and I have a GetTasks method in the DomainService that I call in开发者_运维百科 my viewmodel.

Any help will be greatly appreciated.


You should have access to those types from the client. You can create a list of the Task entity on your ViewModel that you can bind to.

private List<Task> _tasks;
public List<Task> Tasks
{
     get { return _tasks; }
     set {
          _tasks = value;
          NotifyPropertyChanged("Tasks");
          }
}

Or you can create a client side poco to map to if you don't want to bind directly to entities.


The following has nothing to do with the "gouping", since it doesn't seem to be your problem.

What I would do is have a property of ObservableCollection<Task>:

public ObservableCollection<Task> Tasks {get; private set;}

which I initialize in the constructor, ask the domain context to load the tasks, and then fill the collection with the data coming in the callback like so:

private TasksDomainContext context;
public TasksViewModel()
{
    Tasks=new ObservableCollection<Task>();
    context= new TasksDomainContext();
    LoadTasks();
}
void LoadTasks()
{
    context.Load(
        context.GetTasksQuery(),
        callback =>
                 {
                    if(callback.HasError)
                    {
                        //handle error
                    }
                    else
                    {
                        Tasks.Clear();
                        foreach(var task in callback.Entities)
                            Tasks.Add(task);
                    }
                 },
         null);
}

Hope this helps ;)

P.S.: if you're having an issue with grouping, please give more details

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜