New to Telerik UI for WPF? Download free 30-day trial

Data Bind to Dynamic Object with CLR and Dynamic Properties

This article describes how to implement DynamicObject with dynamic (DLR) and static (CLR) fields, and data bind it to RadGridView.

The following model shows a class that derives from DynamicObject and containing one CLR property called Id. When the RadGridView auto-generates its columns, the TryGetMember method of the DynamicObject class will be used to fetch the values for each column. This said, you will need to implement some logic in the method in order to allow RadGridView to work with the data - both CLR (Common Language Runtime) and DLR (Dynamic Language Runtime).

Example 1: DynamicObject implementation

public class RowInfo : DynamicObject, INotifyPropertyChanged 
{      
    private int id;  
    private readonly IDictionary<string, object> data = new Dictionary<string, object>(); 
 
    public event PropertyChangedEventHandler PropertyChanged; 
 
    // CLR property 
    public int Id 
    { 
        get { return id; } 
        set { id = value; OnPropertyChanged("Id"); } 
    } 
 
    public override IEnumerable<string> GetDynamicMemberNames() 
    { 
        return data.Keys; 
    } 
 
    public override bool TryGetMember(GetMemberBinder binder, out object result) 
    { 
        if (binder.Name == nameof(this.Id)) 
        { 
            result = this.Id; 
        } 
        else 
        { 
            result = this[binder.Name]; 
        } 
 
        return true; 
    } 
 
    public override bool TrySetMember(SetMemberBinder binder, object value) 
    { 
        this[binder.Name] = value; 
        return true; 
    } 
 
    public object this[string columnName] 
    { 
        get 
        { 
            if (data.ContainsKey(columnName)) 
            { 
                return data[columnName]; 
            } 
            return null; 
        } 
        set 
        { 
            if (!data.ContainsKey(columnName)) 
            { 
                data.Add(columnName, value); 
                OnPropertyChanged(columnName); 
            } 
            else 
            { 
                if (data[columnName] != value) 
                { 
                    data[columnName] = value; 
                    OnPropertyChanged(columnName); 
                } 
            } 
        } 
    } 
 
    private void OnPropertyChanged(string propertyName) 
    { 
        if (PropertyChanged != null) 
        { 
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
        } 
    } 
} 

Example 2: Populating with data

public MyUserControl() 
{ 
    InitializeComponent(); 
 
    var data = new ObservableCollection<RowInfo>(); 
    for (int i = 0; i < 100; i++) 
    { 
        var row = new RowInfo() { Id = i };                 
        for (int j = 0; j < 5; j++) 
        { 
            row[string.Format("Column{0}", j)] = string.Format("Cell {0} {1}", i, j); 
        } 
 
        data.Add(row); 
    } 
 
    this.DataContext = data; 
} 

Example 3: RadGridView definition

<telerik:RadGridView ItemsSource="{Binding}" /> 
WPF RadGridView with Mixed CLR and Dynamic Properties

Another code example with dynamic data can be found in the Various Data Sources demo.

In this article