title: Xamarin datatemplate with bindings in code behind not working
body: <p>I have tried to make a page in my app where all controls are generated dynamically via C# code behind. I am using a Nuget Packages, <code>DLToolkit, flowlist</code> to create a flow list.</p>
  
  <p>I have already used this package in my project before using <strong>Xaml</strong>, and it fully works.
  However when I try to create a <code>datatemplate</code> in code behind, it just displays a blank control, however when hovering above this control, you can see there's actually items in it.</p>
  
  <p><strong>My question is: How can I make a datatemplate with databindings in code behind?</strong> </p>
  
  <p>Here is an example and works in <strong>Xaml</strong>:</p>
  
  <pre><code>&lt;flv:FlowListView x:Name="flvList" BackgroundColor="White" FlowColumnCount="3" FlowItemsSource="{Binding LstItemSource}" HasUnevenRows="True"&gt;
              &lt;flv:FlowListView.FlowColumnTemplate&gt;
                  &lt;DataTemplate&gt;
  
                      &lt;StackLayout BackgroundColor="White" Padding="2" HorizontalOptions="FillAndExpand"&gt;
  
                          &lt;Frame  Margin="20" Padding="0" HeightRequest="175" OutlineColor="Gray" BackgroundColor="White" CornerRadius="10" HasShadow="True" IsClippedToBounds="True"&gt;
                              &lt;Frame.Content&gt;
  
                                  &lt;AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" &gt;
                                      &lt;Image Aspect="AspectFill" AbsoluteLayout.LayoutBounds="1,1,1,1" AbsoluteLayout.LayoutFlags="All"  Source="{Binding BgImage}" /&gt;
                                      &lt;BoxView Color="Black" Opacity=".5"  AbsoluteLayout.LayoutBounds="1,1,1,1" AbsoluteLayout.LayoutFlags="All"/&gt;
                                      &lt;StackLayout Margin="20" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"&gt;
                                          &lt;Label Text="{Binding SubTitle}"  FontSize="Medium" TextColor="#66FFFFFF"/&gt;
                                          &lt;Label Text="{ Binding Title}" FontSize="Large" TextColor="White" /&gt;
                                      &lt;/StackLayout&gt;
                                  &lt;/AbsoluteLayout&gt;
  
                              &lt;/Frame.Content&gt;
                          &lt;/Frame&gt;
  
                      &lt;/StackLayout&gt;
  
                  &lt;/DataTemplate&gt;
              &lt;/flv:FlowListView.FlowColumnTemplate&gt;
          &lt;/flv:FlowListView&gt;
  </code></pre>
  
  <p>However, in this project the controls are generated, so there is no <strong>Xaml</strong> code involved.
  This is an example of the code that I've tried in code behind, but doesn't work:</p>
  
  <pre><code>#region Datatemplate
              var dataTemplate = new DataTemplate(() =&gt;
              {
                  var StackLayout = new StackLayout { BackgroundColor = Color.Pink, Padding = 2, HorizontalOptions = LayoutOptions.FillAndExpand };
  
                  #region children/content for frame
                  AbsoluteLayout absoluteLayout = new AbsoluteLayout { HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand };
  
  
                  #region content for AbsoluteLayout
                  var imgBg = new Image();
                  AbsoluteLayout.SetLayoutBounds(imgBg , new Rectangle(1, 1, 1, 1));
                  AbsoluteLayout.SetLayoutFlags(imgBg , AbsoluteLayoutFlags.All);
  
                  imgBg .SetBinding(Image.SourceProperty, "BgImage");
                  absoluteLayout.Children.Add(imgBg );
  
  
                  var overlayBox = new BoxView { Color = Color.Black, Opacity = 0.5 };
                  AbsoluteLayout.SetLayoutBounds(overlayBox, new Rectangle(1, 1, 1, 1));
                  AbsoluteLayout.SetLayoutFlags(overlayBox, AbsoluteLayoutFlags.All);
                  absoluteLayout.Children.Add(overlayBox);
  
  
                  #region InnerStackpanel
                  StackLayout innerStackVoorAbsoluteLayout = new StackLayout { Margin = new Thickness(20), VerticalOptions = LayoutOptions.CenterAndExpand, HorizontalOptions = LayoutOptions.CenterAndExpand };
  
                  var lblTitel = new Label { FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)), TextColor = Color.White };
                  var lblSubTitel = new Label { FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)), TextColor = Color.White };
  
  
                  //Bindings 
                  lblTitel.SetBinding(Label.TextProperty, "Title");
                  lblSubTitel.SetBinding(Label.TextProperty, "SubTitle");
  
                  innerStackVoorAbsoluteLayout.Children.Add(lblSubTitel);
                  innerStackVoorAbsoluteLayout.Children.Add(lblTitel);
  
                  absoluteLayout.Children.Add(innerStackVoorAbsoluteLayout);
                  #endregion
  
                  #endregion
  
  
                  #endregion
  
                  Frame frame = new Frame();
                  frame.Content = absoluteLayout;
  
  
  
                  StackLayout.Children.Add(frame);
  
  
                  return StackLayout;
              });
  
              #endregion
  
  
              FlowListView lstRelatieLijst = new FlowListView();
              lstRelatieLijst.ItemsSource = lstRelatieItems;
              lstRelatieLijst.FlowColumnTemplate = dataTemplate;
              lstRelatieLijst.BackgroundColor = Color.LightGoldenrodYellow;
              lstRelatieLijst.FlowColumnCount = 1;
              lstRelatieLijst.HasUnevenRows = true;
  
  
              #endregion
  </code></pre>
  
  <p>Can someone give me some advice how I can become something similar like the upper <strong>Xaml</strong> in code behind, please?</p>
  
  <p>I already tried the following sources but unfortunately I doesn't work as expected. I hoped to see the same result or something similar like the XAML code. But after following their info,the FLowListView appears to be empty:</p>
  
  <p><a href="https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/creating" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/creating</a> </p>
  
  <p><a href="https://www.codeproject.com/Questions/516614/createplusdatatemplatepluscodeplusbehind" rel="nofollow noreferrer">https://www.codeproject.com/Questions/516614/createplusdatatemplatepluscodeplusbehind</a></p>