简体   繁体   English

通过(列)名称而不是通过硬编码索引获取相关的ListViewSubItem?

[英]Get the relevant ListViewSubItem by (column) name and not by hardcoding index?

I have a ListViewItem extension method wrapper that gets the relevant SubItem reference by index (hard-coded), i want to know if there's a way to get it by not hard-coding the index number. 我有一个ListViewItem 扩展方法包装,它通过索引(硬编码)获取相关的SubItem引用,我想知道是否有一种方法可以通过不对索引号进行硬编码来获取它。

public class ListViewExtensionsLogic : IListViewExtensionsLogic
{
    public ListViewItem.ListViewSubItem GetUserIdSubitem(ListViewItem lvi)
    {
        return lvi.SubItems[1]; // I want to get it by not hard-coding it
    }
}

public static class ListViewExtensionMethods
{
        private static IListViewExtensionsLogic _listviewExtensionsLogic;

        static ListViewExtensionMethods()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<ListViewExtensionsLogic>().As<IListViewExtensionsLogic>();

            var container = builder.Build();
            container.BeginLifetimeScope();

            _listviewExtensionsLogic = container.Resolve<IListViewExtensionsLogic>();
        }

        public static ListViewItem.ListViewSubItem GetUserIdSubItem(this ListViewItem lvi)
        {
            return _listviewExtensionsLogic.GetUserIdSubitem(lvi);
        }
}

Being used like this: 被这样使用:

        private void InitializeFollowers()
        {
            if (lvFollowList.Items.Count > 0)
            {
                foreach (ListViewItem lvi in lvFollowList.Items)
                {
                    if (!String.IsNullOrEmpty(lvi.GetUserIdSubItem().Text))
                    {
                        // Do something here
                    }
                }
            }
        }

why don't you pass the index as well as a parameter to the method like below 为什么不将索引以及参数传递给如下所示的方法

public ListViewItem.ListViewSubItem GetUserIdSubitem(ListViewItem lvi, int index)
{
    return lvi.SubItems[index];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM