Binding in code

ysteinSelbekkysteinSelbekk NOMember, University

Hi, in this documentation there are example code for binding using devexpress gridcontrol. In the Xaml:MainPage.xaml tab could someone explain this binding:

<Grid BindingContext="{Binding Source}" Padding="0,15,0,15">

and how to do this in code?
I have tried the following:

templateColumn.DisplayTemplate = new DataTemplate (() => { Label firstNameLabel = new Label(); firstNameLabel.FontAttributes = FontAttributes.Bold; firstNameLabel.FontSize = 20; firstNameLabel.SetBinding(Label.TextProperty, new Binding("FirstName")); Label emailLabel = new Label(); emailLabel.SetBinding(Label.TextProperty, new Binding("Email")); Grid grid = new Grid { RowDefinitions = { new RowDefinition() { Height = GridLength.Auto }, new RowDefinition() { Height = GridLength.Auto } }, Children = { firstNameLabel, emailLabel } }; grid.SetBinding(Grid.BindingContextProperty, new Binding("Source")); return grid; });

but I keep getting this error:

System.NullReferenceException: Object reference not set to an instance of an object at at DevExpress.Mobile.DataGrid.Internal.ObjectRowData

Best Answer


  • ysteinSelbekkysteinSelbekk NOMember, University

    Thanks for the quick answer.

  • ysteinSelbekkysteinSelbekk NOMember, University

    Finally figured it out :) The following link had close to what I was after

    I ended up with this:

    GridControl gridControl = new GridControl (); gridControl.SetBinding (GridControl.ItemsSourceProperty, new Binding("Customers")); gridControl.Columns.Add (new TemplateColumn() { FieldName = "FirstName", DisplayTemplate = new DataTemplate(typeof(CustomerListItemView)) });

    Where CustomerListItemView inherits StackLayout (I changed from using Grid to StackLayout)
    which has the following constructor++

    public CustomerListItemView() { SetBinding(BindingContextProperty, new Binding("Source")); firstNameLabel = new Label(); ... } protected override void OnBindingContextChanged () { base.OnBindingContextChanged (); firstNameLabel.SetBinding (Label.TextProperty, new Binding ("FirstName")); ... }
Sign In or Register to comment.