2017-12-25 16 views
1

私のXamarin.Forms XAMLファイルにはリストがあり、その中にDataTemplateImageCellを使って項目を表示しています。これまでは、2つのテキストと各アイテムの画像を表示する方法を理解しましたが、ボタンも追加したいと考えています。ListViewのDataTemplateに要素を追加するにはどうすればよいですか?

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

答えて

2

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

あなたはImageCellが提供するものだけで、その後の行に、より多くのデータが欲しいので、あなたはあなたのListView行に表示されているものを構築するためにViewCellを使用することができます。

例:

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

imagcellの代わりにviewcellを使用できます。そしてviewcellにuがViewCellにカスタム要素を追加

+0

私はXAMLで右または私はコードビハインドでそれをしなければならないかがあることを行うことができますか? –

+0

これはxamlで実行できます。 – qubuss

1

代わりのImageCellは、あなたが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> 
関連する問題