ContentView For ListView Item

FlorianMICHALONFlorianMICHALON FRMember ✭✭✭
edited May 2016 in Xamarin.Forms

Hi all,

I want to use a content view for item temple inside my list view.

my code :

ListView lister;
lister = new ListView();
lister.ItemTemplate = new DataTemplate(typeof(MyBandNewsSuperCell));
lister.ItemsSource = newslist;

and MyBandNewsSuperCell is a ContentView with xaml here is code:

namespace App
{
    public partial class MyBandNewsSuperCell : ContentView
    {
        public MyBandNewsSuperCell()
        {
            InitializeComponent();

            profilimg.SetBinding(RoundedImageView.SourceProperty, "UserAvatar");
            hourlabel.SetBinding(Label.TextProperty, "Hour");
            firstnamelabel.SetBinding(Label.TextProperty, "FirstNameUser");
            lastnamelabel.SetBinding(Label.TextProperty, "LastNameUser");

        }
    }
}

And here is xaml :

<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App.MyBandNewsSuperCell"
             xmlns:local="clr-namespace:App;assembly=App"
             xmlns:i18n="clr-namespace:App<del></del>;assembly=App">
  <ContentView.Content>
    <Grid x:Name="maingrid" AbsoluteLayout.LayoutBounds="0,0,1,0.2" AbsoluteLayout.LayoutFlags="All" BackgroundColor="{StaticResource ColorWhite}" RowSpacing="1" IsVisible="true" ColumnSpacing="0">
      <Grid.RowDefinitions>
        <RowDefinition Height="2*" />
        <RowDefinition Height="28*" />
        <RowDefinition Height="68*" />
        <RowDefinition Height="2*" />
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*" />
        <ColumnDefinition Width="15*" />
        <ColumnDefinition Width="20*" />
        <ColumnDefinition Width="61*" />
        <ColumnDefinition Width="2*" />
      </Grid.ColumnDefinitions>
      <BoxView BackgroundColor="{StaticResource ColorGrey}" Opacity="0.7" Grid.Row="0" Grid.Column="0" Grid.RowSpan="4" Grid.ColumnSpan="5" />
      <Image x:Name="profilimg" Grid.Row="1" Grid.RowSpan="2" Grid.Column="1" />
      <Label x:Name="hourlabel" Style="{StaticResource BlackColorNormalSize}" Grid.Row="1" Grid.Column="2"/>
      <StackLayout Orientation="Vertical" Grid.Row="2" Grid.Column="2" >
        <Label x:Name="firstnamelabel" Style="{StaticResource FontWhiteAndCenter}"/>
        <Label x:Name="lastnamelabel" Style="{StaticResource FontWhiteAndCenterLittleFont}"/>
      </StackLayout>
    </Grid>
  </ContentView.Content>
</ContentView>

But when i compile i had an error "Specified cast is not valid.", any one have an idea for resolve my problem?

Sorry for my english and thanks in advance.

Posts

Sign In or Register to comment.