How can I create a Grid with it's X and Y axis bound to different collections?

元气小坏坏 提交于 2019-12-01 01:28:05

I think I might have found a way to do it... I need to rearrange my Data a bit

  • My main ViewModel will contain ObservableCollection<DateTime> CurrentWeek and ObservableCollection<Category> CurrentTasks

  • The Category class should have ObservableCollection<Task> Tasks

  • And Task should have ObservableCollection<TaskInstances> Occurences

The main ViewModel should also subscribe to CurrentWeek.CollectionChanged, and based on what the dates are in the collection, update each Category.Task[x].Occurences if Category.Task.IsRecurring == true

Then I think I can use the following structure to get what I want. I'll update this post when I know if this works or not

Update: It works :)

I'd probably go with a DataTable property that you rebuild when you change weeks, then bind a DataGrid to that DataTable.

Rebuild code would look something like this (will obviously need tweaking based on your collection properties):

tbl = new DataTable();
foreach(var day in Week) {
    tbl.Columns.Add(new DataColumn(day.Name, Task));
}

foreach(var cat in Categories) {
    var tasks = AllTasks.Where(t => t.Category.equals(cat) && Week.Contains(t.Day));
    if (tasks.Any()) {
        foreach (var task in tasks) { 
            var row = new DataRow();
            row.SetField(day.Name, task);
            tbl.Rows.Add(row);
        }
    }
}

Then you could bind a DataGrid to that DataTable:

<!-- Wherever you want the datagrid -->
<DataGrid ItemsSource="{Binding, NotifyOnSourceUpdated=True}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header={Binding Columns[0].Name} CellTemplate="{StaticResource TaskTemplate}"/>
        <!-- Six more columns for the other days -->
    </DataGrid.Columns>
</DataGrid>

Considering that we are talking about WPF, I assume that Viewed Week, Categories, and the Tasks are objects of the model. So what I would do in this case, is to create ModelView that combines that objects based on your app logic relationship into another ModelView's Week object, whom properties actually end up binded to actual UI (Grid, ListView, Telerik control, whatever...).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!