简体   繁体   中英

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.

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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