以下のように簡単なMultiSelectListView
を作成しましたが、何とかItemSelected
イベントが発生しません。MultiSelectable ListView - ItemSelectedが呼び出されていません
私はすべてを試したと思うので、間違っていると教えてもらえますか?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Xamarin.Forms;
namespace <myProject>.Views
{
class MultiSelectListView<T> : ContentView
{
private ListView _lstView;
private IEnumerable<WrappedSelection<T>> _wrappedItems;
public IEnumerable<T> Items
{
get { return _wrappedItems?.Select(i => i.Item) ?? new T[0]; }
set
{
_wrappedItems = value?.Select(item => new WrappedSelection<T> { Item = item });
_lstView.ItemsSource = _wrappedItems;
}
}
public MultiSelectListView(string bindingProperty, IEnumerable<T> items = null)
{
if (string.IsNullOrWhiteSpace(bindingProperty))
throw new ArgumentNullException(nameof(bindingProperty));
WrappedItemSelectionTemplate.BindingProperty = bindingProperty;
Content = _lstView = new ListView(ListViewCachingStrategy.RecycleElement)
{
ItemTemplate = new DataTemplate(typeof(WrappedItemSelectionTemplate))
};
_lstView.ItemSelected += (sndr, e) =>
{
if (e.SelectedItem == null)
return;
var wrappedSelection = e.SelectedItem as WrappedSelection<T>;
if (wrappedSelection != null)
wrappedSelection.IsSelected = !wrappedSelection.IsSelected;
var lstView = sndr as ListView;
if (lstView != null)
lstView.SelectedItem = null;
};
Items = items;
}
}
class WrappedItemSelectionTemplate : ViewCell
{
public static string BindingProperty { get; internal set; }
public WrappedItemSelectionTemplate()
{
var name = new Label { LineBreakMode = LineBreakMode.WordWrap, Style = Styles.GetTextStyle() };
name.SetBinding(Label.TextProperty, new Binding($"Item{(!string.IsNullOrEmpty(BindingProperty) ? $".{BindingProperty}" : "")}"));
var grid = new Grid
{
Children = { name },
ColumnDefinitions = { new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) } }
};
View = grid;
}
}
class WrappedSelection<T> : INotifyPropertyChanged
{
private bool _isSelected;
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public bool IsSelected
{
get { return _isSelected; }
set
{
if (_isSelected != value)
{
_isSelected = value;
PropertyChanged(this, new PropertyChangedEventArgs(nameof(IsSelected)));
}
}
}
public T Item { get; set; }
}
}
xamlを共有できますか? – Uros
私はXAMLを持っていませんか? –