This is a migrated thread and some comments may be shown as answers.

Bind to list of rows with list of cells, and custom cell templates.

2 Answers 74 Views
GridView
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 27 Aug 2010, 12:13 PM
In our scenario the data structure we need to bind to looks something like this (simplified):

public interface IEvent
{
    ObservableCollection<IRow> Rows { get; }
    ObservableCollection<IColumn> Columns { get; }
}
 
public interface IColumn
{
    DataTemplate Template { get; }
}
 
public interface IRow
{
    ObservableCollection<ICell> Cells { get; }
}
 
public interface ICell
{
    string Value { get; }
}

The IEvent is the DataContext for the grid.

From what I can tell this scenario isn't supported through XAML, so I'm building up the grid with its list of columns in code-behind.  I can set up the DataMemberBinding for each column to something like this:

column.DataMemberBinding = new Binding("Cells[" + columnIndex + "].Value");

It's not ideal, but it works correctly and I get the ICell's value in the cell on the grid.  Now I want to use a custom DataTemplate for each column.  This is where I am struggling to find a neat solution.

Let's say we have a type implementing ICell that exposes some extra information:

public class CustomCell : ICell
{
    public string Value { get; }
    public string CustomData { get; set; }
}

Let's also assume this is implementing INotifyPropertyChanged.  My custom DataTemplate for a cell might look something like this:

<StackPanel>
    <TextBlock Text="{Binding Value}" FontWeight="Bold" Foreground="Green" />
    <TextBlock Text="{Binding CustomData}" FontWeight="Bold" Foreground="Red" />
</StackPanel>

But this won't work because the data context for the template is the IRow, not the ICell.  I think can't create a template based on the IRow because the template doesn't know the column index:

<StackPanel>
    <TextBlock Text="{Binding Cells[???].Value}" FontWeight="Bold" Foreground="Green" />
    <TextBlock Text="{Binding Cells[???].CustomData}" FontWeight="Bold" Foreground="Red" />
</StackPanel>

This is where I'm getting a bit stuck.  Is there a way to achieve what I want with RadGridView?  We can change our data structures if necessary.

To give a bit of background, each column is defined by an add-in and the add-in also provides the DataTemplate for its data.  This is why the columns need to be defined at run time and the template is concerned about the cell and not the entire row.

Thanks,
James.

2 Answers, 1 is accepted

Sort by
0
Accepted
Milan
Telerik team
answered on 03 Sep 2010, 09:12 AM
Hello James,

One possible approach is to use a custom control in your CellTemplates that will allow you to drop the complex bindings ({Binding Cells[???].Value}) and simply use {Binding Value}.

I have attached a sample application that demonstrates this approach.


Kind regards,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
James
Top achievements
Rank 1
answered on 03 Sep 2010, 11:43 AM
Thanks Milan, I've used your technique in my prototype and it works perfectly.

Cheers,
James.

Tags
GridView
Asked by
James
Top achievements
Rank 1
Answers by
Milan
Telerik team
James
Top achievements
Rank 1
Share this question
or