This is a migrated thread and some comments may be shown as answers.

Problem changing template to listview

1 Answer 143 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Fabio
Top achievements
Rank 1
Fabio asked on 27 Sep 2016, 05:10 PM

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

1 Answer, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 29 Sep 2016, 05:24 PM
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
Tags
ListView
Asked by
Fabio
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Share this question
or