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:

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

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

<br />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 = {

    grid.SetBinding(Grid.BindingContextProperty, new Binding("Source"));

    return grid;

but I keep getting this error:

<br />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:

    <br />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++

    <br />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.