internal static List <T> CollectCheckedObjects <T>(this FastObjectListView listView, ObjectListViewCollector returnIfNoneChecked = ObjectListViewCollector.All)
        {
            if (listView.Objects == null || listView.GetItemCount() == 0)
            {
                return(new List <T>());
            }

            List <T> list = new List <T>();

            if (listView.CheckedObjects.Count > 0)
            {
                foreach (var ch in listView.CheckedObjects)
                {
                    list.Add((T)ch);
                }
            }
            else
            {
                switch (returnIfNoneChecked)
                {
                case ObjectListViewCollector.Empty: return(list);

                case ObjectListViewCollector.Selected:
                    if (listView.SelectedObject != null)
                    {
                        list.Add((T)listView.SelectedObject);
                    }
                    break;

                case ObjectListViewCollector.All:
                    foreach (var item in listView.Objects)
                    {
                        list.Add((T)item);
                    }
                    break;
                }
            }
            return(list);
        }
示例#2
0
        public static void UpdateFOLV(FastObjectListView olv, IList objs,
                                      bool Follow = false,
                                      ColumnHeaderAutoResizeStyle ResizeColsStyle = ColumnHeaderAutoResizeStyle.None,
                                      bool FullRefresh     = false,
                                      bool UseSelected     = false,
                                      object SelectObject  = null,
                                      bool ForcedSelection = false,
                                      [CallerMemberName()] string memberName = null)
        {
            Global_GUI.InvokeIFRequired(olv, () =>
            {
                try
                {
                    int oldcnt = olv.Items.Count;

                    if (oldcnt == 0)
                    {
                        olv.EmptyListMsg = "Loading...";
                    }

                    //Debug.Print($"{DateTime.Now} - {memberName} > UpdateFOLV: {objs.Count} items, Follow={Follow}");

                    olv.Freeze();  //accessed from another thread

                    if (FullRefresh)
                    {
                        //olv.ClearCachedInfo();
                        //olv.ClearObjects();
                    }

                    if (FullRefresh || olv.Items.Count == 0)  //full refresh of new objects
                    {
                        olv.SetObjects(objs, true);
                    }
                    else
                    {
                        olv.AddObjects(objs);  //minimize list changes
                    }
                    if (olv.Items.Count > 0)
                    {
                        if (Follow)
                        {
                            if (SelectObject != null && UseSelected)
                            {
                                //use the given object as selected
                                olv.SelectedObject = SelectObject;  //olv.Items.Count - 1;
                            }
                            else if (SelectObject == null && UseSelected && olv.IsFiltering && olv.GetItemCount() > 0)
                            {
                                //use the last filtered object as the selected object
                                olv.SelectedObject = olv.FilteredObjects.Cast <object>().Last();
                            }
                            else if (!olv.IsFiltering)
                            {
                                //just use the last object given to us
                                olv.SelectedObject = objs[objs.Count - 1];  //olv.Items.Count - 1;
                            }


                            if (olv.SelectedObject != null)
                            {
                                olv.EnsureModelVisible(olv.SelectedObject);
                            }
                        }
                        else
                        {
                            //only select something if nothing is already selected
                            if (SelectObject != null && (ForcedSelection || (UseSelected && olv.SelectedObject == null)))
                            {
                                //use the given object as selected
                                olv.SelectedObject = SelectObject;  //olv.Items.Count - 1;
                                olv.EnsureModelVisible(olv.SelectedObject);
                            }
                            //else if (SelectObject == null && UseSelected && olv.IsFiltering && olv.GetItemCount() > 0)
                            //{
                            //    //use the last filtered object as the selected object
                            //    olv.SelectedObject = olv.FilteredObjects.Cast<object>().First();
                            //}
                            //else if (!olv.IsFiltering && UseSelected)
                            //{
                            //    //just use the last object given to us
                            //    olv.SelectedObject = objs[0];  //olv.Items.Count - 1;
                            //}
                        }


                        //update column size only if did not restore folv state file or forced
                        if (olv.Tag == null)
                        {
                            olv.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                            olv.Tag = "resizedcols";
                        }
                        else if (ResizeColsStyle != ColumnHeaderAutoResizeStyle.None)
                        {
                            olv.AutoResizeColumns(ResizeColsStyle);
                            olv.Tag = "resizedcols";
                        }
                    }
                    else
                    {
                        olv.EmptyListMsg = "Empty";
                    }
                }
                catch (Exception ex)
                {
                    Log("Error: " + ex.Msg());
                }
                finally
                {
                    olv.Unfreeze();
                    if (FullRefresh)
                    {
                        olv.Refresh();
                    }
                }
            });
        }