Problem changing template to listview

2 posts, 0 answers
  1. Fabio
    Fabio avatar
    5 posts
    Member since:
    Sep 2013

    Posted 27 Sep Link to this post

    Hello, I have problems trying to apply different templates to my RadListView on Xamarin Forms.

    If I declare my templates as resources on my xaml, i.e.:

    <ResourceDictionary>
          <DataTemplate x:Key="templatePortrait" x:Name="templatePortrait">
            <telerikListView:ListViewTemplateCell>
              <telerikListView:ListViewTemplateCell.View>
              <Grid VerticalOptions="FillAndExpand" BackgroundColor="{StaticResource LightGrayBackgroundColor}">
                <Grid.RowDefinitions>
                  <RowDefinition Height="50*" />
                  <RowDefinition Height="30*" />
                  <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="60*"/>
                  <ColumnDefinition Width="40*"/>
                </Grid.ColumnDefinitions>

        </Grid>
            </telerikListView:ListViewTemplateCell.View>
            </telerikListView:ListViewTemplateCell>
          </DataTemplate>

     

     <DataTemplate x:Key="templateLandscape" x:Name="templatePortrait">
            <telerikListView:ListViewTemplateCell>
              <telerikListView:ListViewTemplateCell.View>
              <Grid VerticalOptions="FillAndExpand" BackgroundColor="{StaticResource LightGrayBackgroundColor}">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="50*"/>
                  <ColumnDefinition Width="50*"/>
                </Grid.ColumnDefinitions>
        </Grid>
            </telerikListView:ListViewTemplateCell.View>
            </telerikListView:ListViewTemplateCell>
          </DataTemplate>

    </ResourceDictionary>

     

    and then I change the item template of my listview when I rotate the device, simply in this way:

    listView.ItemTemplate = templateLandscape;

    or

    listView.ItemTemplate = templatePortrait;

     

    it doesn't work, seems that the new template is not applied.

     

    Instead if I assign the itemtemplate in this way:

    listView.ItemTemplate = new DataTemplate(typeof(Templates.TelerikLandscape));

     

    all works as expected. Why?

    Where is my mistake?

    Thank you

  2. Lance | Tech Support Engineer, Sr.
    Admin
    Lance | Tech Support Engineer, Sr. avatar
    137 posts

    Posted 29 Sep Link to this post

    Hello Fabio,

    There is nothing wrong with how you're assigning the DataTemplate programmatically, however I do see an issue with your template names. 

    You do not have a DataTemplate in the XAML named templateLandscape (they are both named templatePortait (a common problem when copy/pasting the same template twice, but forget to update the the names). Here's your code:

    <DataTemplate x:Key="templateLandscape" x:Name="templatePortrait"> ...


    Once you change templatePortait to templateLandscape, it should work as expected..

    Regards,
    Lance | Tech Support Engineer, Sr.
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft banner
Back to Top