コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
using Xamarin.Forms;
using AppName.Controls;
using AppName.ViewModels;
using AppName.Models.Converter;
namespace AppName.Controls
{
public class ListViewCellTest : ViewCell
{
//コントロール定義
private Image img = new Image();
private Label lblName1 = new Label();
private Label lblName2 = new Label();
private Label lblNotes = new Label();
private Label lblId = new Label();
private Label lblEnabled = new Label();
private BaseCheckBox chkBox = new BaseCheckBox();
StackLayout baseLayout = null;
StackLayout layout1 = null;
StackLayout layout2 = null;
StackLayout layout3 = null;
StackLayout layout4 = null;
MenuItem actionDelete = null;
//コンストラクタ
public ListViewCellTest(Page page, bool isBaseCheckBoxShowing = false)
{
try
{
this.Initialize(page);
if (isBaseCheckBoxShowing)
{
if (baseLayout == null)
{
baseLayout = new StackLayout
{
Padding = new Thickness(5),
Orientation = StackOrientation.Horizontal, //横に並べる
Children = { chkBox, img, layout1 },
};
}
View = baseLayout;
}
else
{
if (baseLayout == null)
{
baseLayout = new StackLayout
{
Padding = new Thickness(5),
Orientation = StackOrientation.Horizontal, //横に並べる
Children = { img, layout1 },
};
}
View = baseLayout;
}
//無効の背景色を変更
View.SetBinding(View.BackgroundColorProperty, new Binding("IsEnabled", BindingMode.Default, new BackColorConverter(), null));
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message + System.Environment.NewLine + ex.StackTrace);
}
}
private void Initialize(Page page)
{
//コントロール定義1
chkBox.IsChecked = false;
chkBox.VerticalOptions = LayoutOptions.Start;
chkBox.SetBinding(BaseCheckBox.IsCheckedProperty, new Binding("IsDeleted"));
chkBox.Clicked += (s, e) =>
{
try
{
if (BindingContext == null)
{
return;
}
var data = (EventInfo)BindingContext;
data.IsDeleted = chkBox.IsChecked;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message + System.Environment.NewLine + ex.StackTrace);
}
};
//コントロール定義2
img.SetBinding(Image.SourceProperty, new Binding("Picture", BindingMode.Default, new ImageSourceFromByteArrayConverter(), null));
img.Aspect = Aspect.AspectFill;
//コントロール定義3
lblName1.FontSize = 16;
lblName1.TextColor = Color.Black;
lblName1.SetBinding(Label.TextProperty, new Binding("Name1"));
lblName1.HorizontalOptions = LayoutOptions.StartAndExpand;
lblName1.VerticalOptions = LayoutOptions.End;
//コントロール定義4
lblName2.FontSize = 16;
lblName2.TextColor = Color.Black;
lblName2.SetBinding(Label.TextProperty, new Binding("Name2"));
lblName2.HorizontalOptions = LayoutOptions.StartAndExpand;
lblName2.VerticalOptions = LayoutOptions.End;
//コントロール定義5
lblNotes.Margin = new Thickness(5, 0, 0, 0);
lblNotes.FontSize = 14;
lblNotes.TextColor = Color.Gray;
lblNotes.SetBinding(Label.TextProperty, new Binding("Notes"));
lblNotes.HorizontalOptions = LayoutOptions.StartAndExpand;
lblNotes.VerticalOptions = LayoutOptions.Center;
//コントロール定義6
lblId.FontSize = 14;
lblId.TextColor = Color.Gray;
lblId.SetBinding(Label.TextProperty, new Binding("Id"));
lblId.HorizontalOptions = LayoutOptions.End;
lblId.VerticalOptions = LayoutOptions.Center;
lblId.IsVisible = false;
//コントロール定義6
lblEnabled.FontSize = 14;
lblEnabled.TextColor = Color.Gray;
lblEnabled.SetBinding(Label.TextProperty, new Binding("IsEnabled", BindingMode.Default, new BoolConverter(), null));
lblEnabled.HorizontalOptions = LayoutOptions.EndAndExpand;
lblEnabled.HorizontalTextAlignment = TextAlignment.End;
lblEnabled.VerticalOptions = LayoutOptions.Center;
if (layout4 == null)
{
layout4 = new StackLayout
{
Orientation = StackOrientation.Horizontal, //横に並べる
Children = { lblNotes, lblId },
};
}
if (layout3 == null)
{
layout3 = new StackLayout
{
Orientation = StackOrientation.Horizontal, //横に並べる
Children = { lblName2, lblEnabled },
};
}
if (layout2 == null)
{
layout2 = new StackLayout
{
Orientation = StackOrientation.Horizontal, //横に並べる
Children = { lblName1 },
};
}
if (layout1 == null)
{
layout1 = new StackLayout
{
Orientation = StackOrientation.Vertical, //縦に並べる
Children = { layout2, layout3, layout4 },
};
}
//アクション定義
actionDelete = new MenuItem
{
// 2つのアクションを追加するため、MenuItemクラスのインスタンスを2つ生成している
Text = "Delete",
// MenuItemオブジェクトのIsDestructiveプロパティをtrueにセットすることで、メニューアイテムの表示が赤色になる(※ただし、iOSのみ)
IsDestructive = true,
};
actionDelete.SetBinding(MenuItem.CommandParameterProperty, new Binding("."));
actionDelete.Clicked += async (s, e) =>
{
// MenuItemオブジェクトのClickedイベントに追加する
try
{
bool ret = await page.DisplayAlert("削除",
"削除してもよろしいですか?",
"OK", "Cancel");
if (ret)
{
//削除するロジックを実装する
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message + System.Environment.NewLine + ex.StackTrace);
}
};
ContextActions.Add(actionDelete);
}
// プロパティが変更された時に呼ばれる
protected override void OnBindingContextChanged()
{
try
{
base.OnBindingContextChanged();
if (BindingContext == null)
{
return;
}
// 表示対象のデータを取得
var data = (EventInfo)BindingContext;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message + System.Environment.NewLine + ex.StackTrace);
}
}
}
}
using Xamarin.Forms;
public class TestPage : ContentPage
{
void OnButtonClick()
{
this.ListView1.ItemTemplate = new DataTemplate(() => new AppName.Controls.ListViewCellTest(this, false));
this.ListView1.ItemsSource = new ObservableCollection<EventInfo>();
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。