在我的 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/