Hi,
I'm disappointed to not successfully bind a column on a custom object: is it possible to display a custom object in a cell of a grid ?
If i use DataMemberBinding jointly with CellTemplate, the dataContext of the gridviewcell is not my property but still the 'row object':
Is GridViewDataColumn's purpose to bind on 'simple properties' ?
Thanks a lot,
Example: The 2nd column doesn't display the button content neither bind the command because the datacontext of the cell is still a 'Row' object and not and 'Item' object.
Code:
public class MyViewModel : Telerik.Windows.Controls.ViewModelBase { public MyViewModel() { this.ItemsSource = new ObservableCollection<Row>() { new Row() { RowName = "Row 1" }, new Row() { RowName = "Row 2" }, new Row() { RowName = "Row 3" }, }; } public ObservableCollection<Row> ItemsSource { get; set; } } public class Row { public Row() { this.Item = new Item(); } public Item Item { get; private set; } public string RowName { get; set; } } public class Item { private static Random rnd = new Random(); public Item() { this.Display = string.Format("Item {0}", rnd.Next(1, 999)); this.FireItem = new Telerik.Windows.Controls.DelegateCommand((object o) => { System.Diagnostics.Debug.WriteLine("Double click on " + this.Display); }); } public string Display { get; set; } public ICommand FireItem { get; set; } }
Xaml :
<Window.Resources> <local:MyViewModel x:Key="MyViewModel" /> <DataTemplate x:Key="ItemTemplate"> <Button Width="75" Height="75" Command="{Binding FireItem}" Content="{Binding Display}" /> </DataTemplate> </Window.Resources> <Grid> <telerik:RadGridView AutoGenerateColumns="False" DataContext="{Binding Source={StaticResource MyViewModel}}" IsReadOnly="True" ItemsSource="{Binding ItemsSource}"> <telerik:RadGridView.Columns> <telerik:GridViewDataColumn DataMemberBinding="{Binding RowName}" Header="Stage" /> <telerik:GridViewDataColumn CellTemplate="{StaticResource ItemTemplate}" DataMemberBinding="{Binding Item}" Header="Item" /> </telerik:RadGridView.Columns> </telerik:RadGridView> </Grid>