Xamarin Forms - Binding Multiple TextCells in one ListView

我怕爱的太早我们不能终老 提交于 2020-01-02 09:18:23

问题


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>

回答1:


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/




回答2:


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.




回答3:


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>



回答4:


TextCell also has a detail attribute :

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


来源:https://stackoverflow.com/questions/25052746/xamarin-forms-binding-multiple-textcells-in-one-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!