如何在 BindableLayout.ItemsSource 中绑定项目的索引 [英] How to bind index of item in BindableLayout.ItemsSource

查看:38
本文介绍了如何在 BindableLayout.ItemsSource 中绑定项目的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一组按钮,将它们在 MyObservableCollection 中的相应项目的索引显示为文本,并使用 CommandParameter 也作为此索引执行命令.我怎样才能做到这一点?

I want a collection of buttons that show their corresponding item's index in MyObservableCollection as Text, and execute a Command with the CommandParameter also being this index. How can I achieve this?

<StackLayout BindableLayout.ItemsSource="{Binding MyObservableCollection}">
  <BindableLayout.ItemTemplate>
    <DataTemplate>
      <Button Text="{Binding [index of the item in MyObservableCollection]}" 
              Command="{Binding MyCommand}"
              CommandParameter="{Binding [index of the item in MyObservableCollection]}" />
    </DataTemplate>
  </BindableLayout.ItemTemplate>
</StackLayout>

推荐答案

所以让我们以正确的非黑客"方式来做你在评论中提到的黑客"事情^^

So lets do the "hacky" thing you mentioned in the comments in the right "un-hacky" way^^

我会按照以下方式构建一些东西.首先我们创建一个 IIndexable 接口:

I would build something along the following lines. First we make an IIndexable interface:

public interface IIndexable
{
    int Index { get; set; }
}

我们现在自己实现 ObservableCollection 如下:

We now make our own implementation of ObservableCollection<T> like this:

public class IndexableObservableCollection<T> : ObservableCollection<T> where T : IIndexable
{
    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        switch (e.Action)
        {
            case NotifyCollectionChangedAction.Add:
            case NotifyCollectionChangedAction.Replace:
            case NotifyCollectionChangedAction.Move:
                for (int i = 0; i < e.NewItems.Count; i++)
                    if (e.NewItems[i] is IIndexable indexableItem)
                        indexableItem.Index = e.NewStartingIndex + i;
                break;
        }
        base.OnCollectionChanged(e);
    }
}

现在我只在这里完成了 switch 中最基本的实现(在这种情况下,switch 可以用 if 语句替换,但这会让你更容易实现所有选项),你会必须自己查看 NotifyCollectionChangedEventArgs 并相应地实施案例.

For now I've only done the most basic implementation in the switch here (granted in this case the switch can be replaced by an if statement, but this makes it easier for you to implement all the options), you'll have to look into the NotifyCollectionChangedEventArgs yourself and implement the cases accordingly.

在此之后,您希望能够显示实现 IIndexable 接口的索引并将它们放在 IndexableObservableCollection 中,这将在以下情况下自动设置它们的索引他们被添加了.在 xaml 中,您可以使用 {Binding Index} 绑定到这个自动设置的索引.

After this just have classes you want to be able to show an index of implement the IIndexable interface and put them in an IndexableObservableCollection and this will automatically set their index when they're added. in xaml you can then just use {Binding Index} to bind to this automatically set index.

希望能帮到你

这篇关于如何在 BindableLayout.ItemsSource 中绑定项目的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆