Skip to content

Instantly share code, notes, and snippets.

@wislon
Last active September 25, 2015 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wislon/8442dd31b60c7ce13e26 to your computer and use it in GitHub Desktop.
Save wislon/8442dd31b60c7ce13e26 to your computer and use it in GitHub Desktop.
Xamarin Forms XAML Dynamic ListView ItemTemplate Selection

Sample Grid XAML DataTemplate with a Grid layout

Has a basic 5-column, 6-row layout, with padding/spacing columns/rows delineated with coloured boxviews (since gridview cells don't have borders, we can't see where one cell stops and another begins)

Note, to get the design-time intellisense for the DataContext binding, you'll need to add the following namespaces to the top of your XAML file (can go beneath the usual xamarin forms namespaces, order isn't really important):

 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 mc:Ignorable="d"
<DataTemplate x:Key="TriageNoteItemTemplate">
  <Grid MinimumHeightRequest="320" 
        d:DataContext="{d:DesignInstance Type=viewModels:NoteViewModel, IsDesignTimeCreatable=False}"
        ColumnSpacing="0" RowSpacing="0" Padding="0"
    >
    
    <Grid.RowDefinitions>
      <RowDefinition Height="16"/>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="16"/>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="*"/>
      <RowDefinition Height="16"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="16"/>
      <ColumnDefinition Width="48"/>
      <ColumnDefinition Width="16"/>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="16"/>
    </Grid.ColumnDefinitions>
    
    <BoxView Grid.Row="0" Grid.Column="0" Color="Fuschia" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="0" Grid.Column="1" Color="Pink" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="0" Grid.Column="2" Color="Fuschia" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="0" Grid.Column="3" Color="Pink" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="0" Grid.Column="4" Color="Fuschia" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    
    <BoxView Grid.Row="1" Grid.Column="0" Color="Pink" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="1" Grid.Column="1" WidthRequest="48" HeightRequest="48" Color="Gray" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="1" Grid.Column="2" Color="Pink" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="1" Grid.Column="4" Color="Pink" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
  

    <!-- TODO make this visible only when the data is actually loaded -->
    <StackLayout Grid.Row="1" Grid.Column="3" Padding="0" Spacing="0" Orientation="Horizontal">
      <Label LineBreakMode="WordWrap" FontSize="14" VerticalOptions="StartAndExpand" HorizontalOptions="StartAndExpand" Text="{Binding Note.Text, StringFormat='Your text was from {0}'}" />
    </StackLayout>
    
    <BoxView Grid.Row="2" Grid.Column="0" Color="Fuschia" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="2" Grid.Column="1" Color="Pink" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="2" Grid.Column="2" Color="Fuschia" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="2" Grid.Column="3" Color="Pink" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="2" Grid.Column="4" Color="Fuschia" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    
    <StackLayout Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="3" Padding="0" Orientation="Horizontal">
      <Label LineBreakMode="WordWrap" FontSize="14" VerticalOptions="StartAndExpand" HorizontalOptions="StartAndExpand" Text="{Binding Note.OtherText, StringFormat='&#x201C;{0}&#x201D;...'}" />
    </StackLayout>

    <BoxView Grid.Row="4" Grid.Column="0" Color="Pink" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="4" Grid.Column="1" Color="Fuschia" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="4" Grid.Column="2" Color="Pink" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="4" Grid.Column="3" Color="Fuschia" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="4" Grid.Column="4" Color="Pink" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    
    <BoxView Grid.Row="5" Grid.Column="0" Color="Fuschia" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="5" Grid.Column="1" Color="Pink" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="5" Grid.Column="2" Color="Fuschia" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="5" Grid.Column="3" Color="Pink" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
    <BoxView Grid.Row="5" Grid.Column="4" Color="Fuschia" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
  
  </Grid>
</DataTemplate>

Sample for building it in code: http://forums.xamarin.com/discussion/17392/how-to-create-complex-bindings (see the downloadable zip file for an extension which you can use to pull out a template made in xaml into a code object)

Also http://forums.xamarin.com/discussion/comment/66502/#Comment_66502 and https://forums.xamarin.com/discussion/comment/118050#Comment_118050 where they discuss how to hook up ContextActions dynamically.

Another option here (looks simpler) https://oren.codes/2014/12/31/datatemplateselector-for-xamarin-forms/ with a link to how to store and retrieve the xaml templates from a xaml resource dictionary: https://github.com/petermajor/Xamarin.Forms.SharedResourceDictionary

And if you start getting "Static resource not found for key" errors, make sure you've got an InitializeComponent() method call in your app's (or page's) constructor as per http://forums.xamarin.com/discussion/31872/application-resources-staticresource-not-found-for-key.

If you've created your xaml resource dictionary after the fact, you may need to set the Custom Tool on the xaml file's properties to MSBuild:Compile as well, but ymmv.

There's some stuff around the missing DataType attribute on the DataTemplate type and other useful fixes/workarounds at http://forums.xamarin.com/discussion/34763/hey-kids-do-you-like-xaml-adventures-in-xaml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment