gpt4 book ai didi

c# - MultiSelectable ListView - ItemSelected 未被调用

转载 作者:行者123 更新时间:2023-11-29 19:18:35 24 4
gpt4 key购买 nike

我已经创建了一个简单的 MultiSelectListView 如下所示,但是不知何故 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; }
}
}

最佳答案

I have created a simple MultiSelectListView as shown below, but somehow the ItemSelected event is not fired.

我已经测试了您的代码并重现了您的问题。我通过转换 _wrappedItems 的类型解决了这个问题进入List<>类型。

内部MultiSelectListView类修改如下代码:

public IEnumerable<T> Items
{
get { return _wrappedItems?.Select(i => i.Item) ?? new T[0]; }
set
{
_wrappedItems = value?.Select(item => new WrappedSelection<T> { Item = item }).ToList();
_lstView.ItemsSource = _wrappedItems;
}
}

ListView.SelectedItem 变化,ItemSelected事件将被触发。但是当使用 IEnumerableLinq 返回的对象SelectedItemListView为空,因此事件不会被正确触发。所以改变 ItemSourceListView(_wrappedItems)到列表将解决问题。

关于c# - MultiSelectable ListView - ItemSelected 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42907881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com