c# - 如何向 ListView 中的 DataTemplate 添加其他元素?

标签 c# xaml xamarin xamarin.forms xamarin.android

在我的 Xamarin.Forms XAML 文件中,我有一个列表,我在 DataTemplate 中使用 ImageCell 来显示项目。到目前为止,我已经想出了如何为每个项目显示两段文本和一张图像,但我还想添加一个按钮。

我尝试在 ImageCell 中放置一些东西,但出现以下错误:

Can not set the content of ImageCell as it doesn't have a ContentPropertyAttribute

我查找了如何使用 ContentPropertyAttribute,但是 documentation并没有真正解释如何使用它。

如何向每个 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>

最佳答案

Data in a ListView is presented in cells. Each cell corresponds to a row of data.

因为您想要连续获得更多数据,而不仅仅是 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>

关于c# - 如何向 ListView 中的 DataTemplate 添加其他元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47966553/

相关文章:

c# - 在ViewModel中修改后,String属性在View中不会更新

c# - AJAX 和 Web Api 发布方法 - 它是如何工作的?

c# - 单击标签以聚焦 WPF 中的另一个控件

c# - WPF 数据网格 : field value in Edit mode not available outside row

c# - 如何从Windows8中的图像文件创建可写位图图像

sqlite - 如果在创建 Easy Table api 之前数据已存在,为什么我的 Xamarin 应用程序不从 Azure 返回数据?

c# - 获取当前 Activity - Xamarin Android

根据属性名称的 C# Linq where 子句

c# - 如何在 Xamarin 中正确保存文件到共享文件夹中?

c# - 在几种情况下为真的单元测试断言