Skip to content

Instantly share code, notes, and snippets.

@tomomo-s
Created June 15, 2017 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomomo-s/46c43f27ecf4be5c177495c659642fd1 to your computer and use it in GitHub Desktop.
Save tomomo-s/46c43f27ecf4be5c177495c659642fd1 to your computer and use it in GitHub Desktop.
XamarinでCustom Cells作成(C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Xamarin.Forms;
namespace TestListViewXaml
{
/// <summary>
/// コレクション一覧
/// </summary>
public class ListViewCollection
{
// 表情(通常、笑う、怒る)
public string Action { get; set; }
// 画像(通常、笑う、怒る)
public string Image { get; set; }
}
/// <summary>
/// ジェネリックコレクション設定
/// </summary>
public class MainPageCtrl : ContentPage
{
/// <summary>
/// ジェネリックコレクション設定
/// </summary>
public ObservableCollection<ListViewCollection> listcollection { get; set; }
/// <summary>
/// メイン処理
/// </summary>
public MainPageCtrl ()
{
listcollection = new ObservableCollection<ListViewCollection>();
ListView listview = new ListView();
listview.ItemTemplate = new DataTemplate(typeof(CustomControlCell));
// コレクションにアイテムを追加
listcollection.Add(new ListViewCollection { Action = "キリッ(`・ω・´)", Image = "Image/normal.png" });
listcollection.Add(new ListViewCollection { Action = "にこにこ(#^^#)", Image = "Image/smile.png" });
listcollection.Add(new ListViewCollection { Action = "ぷんぷんヽ(`Д´)ノ", Image = "Image/angry.png" });
// ListViewに設定
listview.ItemsSource = listcollection;
Content = listview;
}
public class CustomControlCell : ViewCell
{
public CustomControlCell()
{
Image image = new Image();
Label actionLabel = new Label();
StackLayout verticaLayout = new StackLayout();
StackLayout horizontalLayout = new StackLayout();
Binding actionbind = new Binding("Action");
Binding imagebind = new Binding("Image");
// バインディング設定
actionLabel.SetBinding(Label.TextProperty, actionbind);
image.SetBinding(Image.SourceProperty, imagebind);
// プロパティ設定
horizontalLayout.Orientation = StackOrientation.Horizontal;
horizontalLayout.HorizontalOptions = LayoutOptions.FillAndExpand;
image.HorizontalOptions = LayoutOptions.FillAndExpand;
actionLabel.FontSize = 20;
// レイアウト配置設定
// アクションラベル配置に、イメージを埋め込む
verticaLayout.Children.Add(actionLabel);
horizontalLayout.Children.Add(verticaLayout);
horizontalLayout.Children.Add(image);
// Xamarin.Forms.View(親)に代入
View = horizontalLayout;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment