/// <summary>
 /// Sets the sub-selection to a single item</summary>
 /// <param name="selectionContext">Sub-selection context</param>
 /// <param name="item">Item to select</param>
 public static void Set(this ISubSelectionContext selectionContext, object item)
 {
     if (item != null)
     {
         selectionContext.SubSelectionContext.SetRange(new[] { item });
     }
 }
        /// <summary>
        /// Clears the sub-selection</summary>
        /// <param name="selectionContext">Sub-selection context</param>
        public static void Clear(this ISubSelectionContext selectionContext)
        {
            if (selectionContext == null)
            {
                throw new ArgumentNullException("selectionContext");
            }

            selectionContext.SubSelectionContext.Clear();
        }
示例#3
0
        /// <summary>
        /// Performs custom actions on MouseDown events</summary>
        /// <param name="e">Mouse event args</param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            // If there is an ISelectionContext implemented for the type of our search results,
            // and the mouse click was on one of the search result list items,
            // then have that ISelectionContext select the instance associated with the clicked search result.
            // Additionally, if the type of our search results implements an ISubSelectionContext, have
            // that ISubSelectionContext select the instance associated with the subitem of the clicked search results
            //
            // (For instance, when each search result ListItem is a DomNode, and contains a SubItem for every DomNode
            // property that matched, this would allow both setting ISelectionContext to the DomNode associated with
            // the clicked ListItem, and setting ISubSelectionContext to the property associated with the clicked SubItem
            // in the row)
            if (m_queryResultContext != null)
            {
                // Is the query result type associated with m_queryResultContext have a way of specifying a selection was made?
                ISelectionContext selectionContext = m_queryResultContext.As <ISelectionContext>();
                if (selectionContext != null)
                {
                    // We can specify a selection was made.  Now check if a selection was actually made.
                    ListViewHitTestInfo hitTestInfo = HitTest(e.Location);
                    if (hitTestInfo != null && hitTestInfo.Item != null)
                    {
                        // A selection was made in the search results.  Specify this selection through ISelectionContext.
                        object tag = hitTestInfo.Item.Tag;
                        selectionContext.Set(tag);

                        // Does the query result type also have a way to specify a sub-selection?
                        ISubSelectionContext subSelectionContext = m_queryResultContext.As <ISubSelectionContext>();
                        if (subSelectionContext != null)
                        {
                            object selectedTag = null;
                            if (hitTestInfo.SubItem != null && hitTestInfo.SubItem.Tag != null)
                            {
                                selectedTag = hitTestInfo.SubItem.Tag; // Specific sub-item clicked, use it
                            }
                            else
                            {
                                // No sub-item clicked, select first sub-item if the selected item has any
                                foreach (var obj in hitTestInfo.Item.SubItems)
                                {
                                    var subItem = obj as ListViewItem.ListViewSubItem;
                                    if (subItem != null && subItem.Tag != null)
                                    {
                                        selectedTag = subItem.Tag;
                                        break;
                                    }
                                }
                            }

                            if (selectedTag != null)
                            {
                                subSelectionContext.Set(selectedTag);
                            }
                            else
                            {
                                subSelectionContext.Clear();
                            }
                        }
                    }
                    else // no selection
                    {
                        selectionContext.Clear();
                    }
                }
            }
        }