如何将其他元素添加到listView中的DataTemplate



在我的xamarin.forms xaml文件中,我有一个列表,并且我在DataTemplate中使用ImageCell来显示项目。到目前为止

我尝试将某些内容放入ImageCell中,但是我有以下错误:

无法设置Imagecell的内容,因为它没有 ContentPropertyAttribute

我查找了如何使用ContentPropertyAttribute,但是该文档并不能真正解释如何使用它。

如何向每个DataTemplate添加其他元素?

    <ListView ItemsSource="{Binding}" x:Name="Results">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ImageCell ImageSource="{Binding picture}" Text="{Binding name}" Detail="{Binding category}">
                    <Button Text="test" />
                </ImageCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

listView中的数据在单元格中显示。每个单元格对应于数据行

由于您需要一排更多的数据,而不是ImageCell提供的数据,您可以使用ViewCell来构造ListView行中显示的内容。

示例:

<DataTemplate>
    <ViewCell>
        <StackLayout Orientation="Horizontal">
            <Image ImageSource="{Binding picture}"/>
            <Label Text="{Binding name}" />
            <Button Text="Details" Command="{Binding DetailCommand}" />
        </StackLayout>
    </ViewCell>
</DataTemplate>

您可以使用ViewCell而代替ImageCell。在ViewCell中,您可以使用布局,并在那里图像,标签和按钮

而不是ImageCell,您应该使用ViewCell&amp;在ViewCell中添加您的自定义元素。

<ListView ItemsSource="{Binding}" x:Name="Results">
    <ListView.ItemTemplate>
        <DataTemplate>
           <ViewCell>
               <StackLayout BackgroundColor="#eee" Orientation="Vertical">
                   <StackLayout Orientation="Horizontal">
                       <Image Source="{Binding picture}" />
                       <Label Text="{Binding name}" TextColor="#f35e20" />
                       <Label Text="{Binding category}" TextColor="#f35e20" />
                       <Button Text="test" />
                   </StackLayout>
               </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

最新更新