大佬教程收集整理的这篇文章主要介绍了如何在 ViewModel 中获取 CollectionView 元素的索引?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要从我的 CollectionVIEw
中获取我点击的元素的索引并在 viewmodel 中使用它。我怎么才能得到它?我是 Xamarin 表单的新手
对此有几个选择;我举几个例子:
for (int i = 0; i < Parent.Childs.Count; i++)
{
String child = (String)Parent.Childs[i];
Childs.Add(new ChildViewModel(i,child));
}
与
public class ChildViewModel
{
public int Index { get; set; }
...
}
foreach (var item in Parent.Childs)
{
Childs.Add(new ChildViewModel(Parent.Childs,child));
}
与
public class ChildViewModel
{
public int Index { get; } => Parent.IndexOf(this);
...
public ChildViewModel(IList<Child> parent)
{
if (parent is INotifyCollectionChanged p)
p.CollectionChanged += ParentCollectionChanged;
}
private void ParentCollectionChanged(object sender,NotifyCollectionChangedEventArgs E)
{
NotifyPropertyChanged(nameof(IndeX));
}
}
此答案中给出了一个 valueconverter 获取项目的示例:https://stackoverflow.com/a/662232/11201993 这不需要您向视图模型添加字段。
以上是大佬教程为你收集整理的如何在 ViewModel 中获取 CollectionView 元素的索引?全部内容,希望文章能够帮你解决如何在 ViewModel 中获取 CollectionView 元素的索引?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。