This question is locked. New answers and comments are not allowed.
I am currently using a RadGridView to bind to a List of Row objects. Let's call them PersonRows. Each PersonRow has on it a collection of Tasks, which can have various states. It is my intention that each column in the RadGridView corresponds to a single element in the collection of tasks on each person. The list of tasks cannot be known at compile time, and is loaded from a database, therefore I have to construct the columns and perform the bindings at runtime. I am using a ValueConverter in order to accomplish the binding. First I construct the column, like so:
The ValueConverter:
By overriding .ToString() on the individual tasks, I can confirm that the columns are each binding to the proper item in the Task collection on each person. The column binding itself seems to be working properly, and is not the problem. The problem comes with the DataTemplate I am adding to the column. The DataTemplate is nothing more than a series of controls to manipulate the task objects (in progress, complete, and so on). However, I can see while debugging that the DataTemplate is using the DataContext of the GridView itself (PersonRow), and not the actual binding on each column (specific Task object). For instance, the DataTemplate binding for "TaskStarted" will attempt to bind to "PersonRow.TaskStarted", but it needs to bind to "Task.TaskStarted." How can I set up the DataTemplate to correctly bind to the Task of each person?
foreach (Task task in this.PersonStatusListingViewModel.ConfiguredTasks){ Telerik.Windows.Controls.GridViewDataColumn taskColumn = new Telerik.Windows.Controls.GridViewDataColumn(); taskColumn.UniqueName = task.ID.ToString(); Binding binding = new Binding(); binding.ConverterParameter = taskColumn.UniqueName; TaskValueConverter conv = new TaskValueConverter(); binding.Converter = conv; taskColumn.DataMemberBinding = binding; taskColumn.CellTemplate = (DataTemplate)this.Resources["TaskTemplate"]; taskColumn.Header = task.Description; PersonGrid.Columns.Add(taskColumn);}The ValueConverter:
public class TaskValueConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { int? currentIndex = null; PersonViewModel person = (PersonViewModel)value; ObservableCollection<TaskViewModel> TaskVMs = person.TaskViewModels; for (int i = 0; i < TaskVMs.Count; i++) { if (TaskVMs[System.Convert.ToInt32(i)].taskID.ToString() == parameter) currentIndex = i; } if (currentIndex != null) { return TaskVMs[System.Convert.ToInt32(currentIndex)]; } else return null; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }By overriding .ToString() on the individual tasks, I can confirm that the columns are each binding to the proper item in the Task collection on each person. The column binding itself seems to be working properly, and is not the problem. The problem comes with the DataTemplate I am adding to the column. The DataTemplate is nothing more than a series of controls to manipulate the task objects (in progress, complete, and so on). However, I can see while debugging that the DataTemplate is using the DataContext of the GridView itself (PersonRow), and not the actual binding on each column (specific Task object). For instance, the DataTemplate binding for "TaskStarted" will attempt to bind to "PersonRow.TaskStarted", but it needs to bind to "Task.TaskStarted." How can I set up the DataTemplate to correctly bind to the Task of each person?