fantaseador
fantaseador

Reputation: 99

Xamarin Forms - Binding Multiple TextCells in one ListView

I am having trouble binding multiple TextCells in a ListView. It works fine if there's only one, but gives XamlParseException on adding more. The same Exception occurs while trying to bind a Label. That's why I had to use a TextCell. What's the solution?

<ListView x:Name="pList">
    <ListView.ItemTemplate>
      <DataTemplate>
        <TextCell x:Name="a" Text="{Binding ReceiverName}" TextColor="White" />
      </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Upvotes: 3

Views: 8401

Answers (4)

dcdroid
dcdroid

Reputation: 691

From your comment on one of the answers, it looks like this is what you want

    <ListView x:Name="pList">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <ViewCell.View>
                <StackLayout>
                  <Label Text="{Binding ReceiverName}" TextColor="White" />
                  <Label Text="{Binding SecondText}" TextColor="White" />
                  <Label Text="{Binding ThirdText}" TextColor="White" />
                </StackLayout>
              </ViewCell.View>          
             </ViewCell>
           </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

This will display 3 Labels vertically.The problem you were having is that the DataTemplate can't have more than one child. The standard way around that is to use a layout control such as StackLayout.

Please see this page for more information: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/controls/layouts/

Upvotes: 4

tdhulster
tdhulster

Reputation: 1621

TextCell also has a detail attribute :

<TextCell x:Name="a" Text="{Binding ReceiverName}" Detail="{Binding AnotherName}" TextColor="White" />

Upvotes: 0

KirtiSagar
KirtiSagar

Reputation: 584

you need to add the "ViewCell" inside the data template like this:

<ListView x:Name="pList">
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <ViewCell.View>
          <TextCell x:Name="a" Text="{Binding ReceiverName}" TextColor="White" />
        </ViewCell.View>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

Upvotes: 0

fredrik
fredrik

Reputation: 6638

Guessing from the code you've pasted I'd say that the problem is that you give the control a name. Remove the a:Name and try again.

If that doesn't help, post the exception details as well.

Upvotes: 0

Related Questions