Xamarin Forms - 在一个ListView中绑定多个TextCell

3

我在绑定ListView中的多个TextCell时遇到了麻烦。如果只有一个,它可以正常工作,但添加更多后会出现XamlParseException。尝试绑定Label时也会出现相同的异常。这就是为什么我不得不使用TextCell的原因。有什么解决方案吗?

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

4

根据您在回答中的评论,看起来这正是您想要的

    <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>

这将会垂直地显示3个标签。你遇到的问题是DataTemplate不能有多个子元素。解决这个问题的通常方式是使用像StackLayout这样的布局控件。

更多信息请参考此页面:http://developer.xamarin.com/guides/cross-platform/xamarin-forms/controls/layouts/


0

你需要在数据模板中添加 "ViewCell",像这样:

<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>

实际上我想要做的是 - <ListView x:Name="pList"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <ViewCell.View> <TextCell x:Name="a" Text="{Binding ReceiverName}" TextColor="White" /> <TextCell x:Name="b" Text="{Binding Address}" TextColor="White" /> </ViewCell.View> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> - fantaseador

0

TextCell 还有一个 detail 属性:

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

0

从您粘贴的代码猜测,问题可能是您给控件命名了。请删除 a:Name 然后再试一次。

如果这样做没有帮助,请同时发布异常详细信息。


删除Name属性并没有产生任何影响。当我尝试在第一个TextCell下方添加另一个TextCell时,出现了以下异常 - 在System.Windows.ni.dll中发生了未处理的类型为“System.Reflection.TargetInvocationException”的异常。我的问题不在于给定的代码,而是在我在第一个TextCell下面添加另一个TextCell时出现的。 - fantaseador
啊,是的 - 你需要在视图中添加文本单元格。尝试用 <StackLayout /> 包围你的两个单元格。 - fredrik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接