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

DataMemberBinding AND CellTemplate on custom object

7 Answers 899 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Cedric
Top achievements
Rank 1
Cedric asked on 27 Nov 2017, 12:14 PM

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>

7 Answers, 1 is accepted

Sort by
0
Cedric
Top achievements
Rank 1
answered on 29 Nov 2017, 02:21 PM
Anyone ?
0
Petar Mladenov
Telerik team
answered on 29 Nov 2017, 04:23 PM
Hi Cedric,

Yes, the DataContext of the Cell remains the "Row" object which is the DataContext of the entire GridViewRow. In the typical scenarios you provide value type property with the DataMemberBinding. In these cases the default cell template uses directly the value of the given property and displays it. When you provide reference type in the DataMemberBinding, you can simply access its members in the CellTemplate the following way:

<DataTemplate x:Key="ItemTemplate">
            <Button Width="75"
                    Height="75"
                    Command="{Binding Item.FireItem}"
                    Content="{Binding Item.Display}" />
        </DataTemplate>

I want to also note that, although you might change the DataContext of the cell programmatically, RadGridView will always try to reset it, so it is generally a bad idea.

Regards,
Petar Mladenov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Cedric
Top achievements
Rank 1
answered on 05 Dec 2017, 04:04 PM

Thanks for the advice about not changing the datacontext

Cedric

0
Onno
Top achievements
Rank 1
answered on 27 Feb 2020, 01:17 PM

Reviving this old thread because I've got the same problem. The CellEditTemplate has a RadComboBox with has a certain DataTemplate to render the items in it. When The cell is in non-edit mode, I would like to use the same DataTemplate to render it of course. But I can't, because the binding is different (I need to go through a 'SelectedItem' property on the row object).

This seems silly, I thought setting DataMemberBinding (to the SelectedItem property) on the Column would change the DataContext to that item so you could use the same DataTemplate.

0
Yoan
Telerik team
answered on 03 Mar 2020, 08:36 AM

Hi Onno,

As my colleague Peter had mentioned, the DataContext behind the cell is the data item which is the DataContext for the whole GridViewRow element. I am afraid that changing GridViewCell's DataContext is not possible since it can lead to undesired behavior in RadGridView.

Can you please give us more details on your exact set up so that I can get a better understanding of it and suggest a viable solution?

Regards,
Yoan
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Onno
Top achievements
Rank 1
answered on 03 Mar 2020, 04:47 PM

Hi Yoan,

 

Thanks for your answer. I've created a demo in which I want to use the same DataTemplate for both the CellTemplate and for the ItemTemplate of the ComboBox that is in the CellEditTemplate. I cannot post it here however, so I've created support ticket 1456237 for it. Can you have a look there?

Regards,

Onno

0
Yoan
Telerik team
answered on 04 Mar 2020, 02:23 PM

Hi Onno,

I have already answered your ticket. May I kindly ask you to continue any further communication on the matter there?

Regards,
Yoan
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Cedric
Top achievements
Rank 1
Answers by
Cedric
Top achievements
Rank 1
Petar Mladenov
Telerik team
Onno
Top achievements
Rank 1
Yoan
Telerik team
Share this question
or