The ItemContainerGenerator provides useful utilities for ItemsControls.
        internal bool IsOnCurrentPage(ListBoxItem item)
        {
            var itemsHostRect = Rect.Empty;
            var listBoxItemRect = Rect.Empty;

            if (_visual == null)
            {
                ItemsControlHelper ich = new ItemsControlHelper(ListBox);
                ScrollContentPresenter scp = ich.ScrollHost == null ? null : ich.ScrollHost.GetVisualDescendants().OfType<ScrollContentPresenter>().FirstOrDefault();
                _visual = (ich.ScrollHost == null) ? null : ((scp == null) ? ((FrameworkElement)ich.ScrollHost) : ((FrameworkElement)scp));
            }

            if (_visual == null)
                return true;

            itemsHostRect = new Rect(0.0, 0.0, _visual.ActualWidth, _visual.ActualHeight);
            //ListBoxItem item = ListBox.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem;
            if (item == null)
            {
                listBoxItemRect = Rect.Empty;
                return false;
            }

            GeneralTransform transform = item.TransformToVisual(_visual);
            listBoxItemRect = new Rect(transform.Transform(new Point()), transform.Transform(new Point(item.ActualWidth, item.ActualHeight)));
            if (!this.IsVerticalOrientation())
            {
                return ((itemsHostRect.Left <= listBoxItemRect.Left) && (listBoxItemRect.Right <= itemsHostRect.Right));
            }

            return ((listBoxItemRect.Bottom + 100 >= itemsHostRect.Top) && (listBoxItemRect.Top - 100 <= itemsHostRect.Bottom));
            //return ((itemsHostRect.Top <= listBoxItemRect.Bottom) && (listBoxItemRect.Top <= itemsHostRect.Bottom));
        }
        internal bool IsVerticalOrientation()
        {
            if (_isVerticalOrientation.HasValue)
                return _isVerticalOrientation.Value;

            ItemsControlHelper ich = new ItemsControlHelper(ListBox);
            StackPanel itemsHost = ich.ItemsHost as StackPanel;
            if (itemsHost != null)
            {
                _isVerticalOrientation = (itemsHost.Orientation == Orientation.Vertical);
                return _isVerticalOrientation.Value;
            }
            VirtualizingStackPanel panel2 = ich.ItemsHost as VirtualizingStackPanel;
            _isVerticalOrientation = ((panel2 == null) || (panel2.Orientation == Orientation.Vertical));
            return _isVerticalOrientation.Value;
        }
 /// <summary>
 /// Builds the visual tree for the
 /// <see cref="T:System.Windows.Controls.TreeView" /> control when a new
 /// control template is applied.
 /// </summary>
 public override void OnApplyTemplate()
 {
     ItemsControlHelper.OnApplyTemplate();
     Interaction.OnApplyTemplateBase();
     base.OnApplyTemplate();
 }
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="T:System.Windows.Controls.TreeView" /> class.
 /// </summary>
 public TreeView()
 {
     DefaultStyleKey    = typeof(TreeView);
     ItemsControlHelper = new ItemsControlHelper(this);
     Interaction        = new InteractionHelper(this);
 }
        /// <summary>
        /// Change whether a TreeViewItem is selected.
        /// </summary>
        /// <param name="itemOrContainer">
        /// Item whose selection is changing.
        /// </param>
        /// <param name="container">
        /// Container of the item whose selection is changing.
        /// </param>
        /// <param name="selected">
        /// A value indicating whether the TreeViewItem is selected.
        /// </param>
        internal void ChangeSelection(object itemOrContainer, TreeViewItem container, bool selected)
        {
            // Ignore any change notifications if we're alread in the middle of
            // changing the selection
            if (IsSelectionChangeActive)
            {
                return;
            }

            object       oldValue = null;
            object       newValue = null;
            bool         raiseSelectionChanged = false;
            TreeViewItem element = SelectedContainer;

            // Start changing the selection
            IsSelectionChangeActive = true;
            try
            {
                if (selected && container != SelectedContainer)
                {
                    // Unselect the old value
                    oldValue = SelectedItem;
                    if (SelectedContainer != null)
                    {
                        SelectedContainer.IsSelected = false;
                        SelectedContainer.UpdateContainsSelection(false);
                    }

                    // Select the new value
                    newValue          = itemOrContainer;
                    SelectedContainer = container;
                    SelectedContainer.UpdateContainsSelection(true);
                    SelectedItem = itemOrContainer;
                    UpdateSelectedValue(itemOrContainer);
                    raiseSelectionChanged = true;

                    // Scroll the selected item into view.  We only want to
                    // scroll the header into view, if possible, because an
                    // expanded TreeViewItem contains all of its child items
                    // as well.
                    ItemsControlHelper.ScrollIntoView(container.HeaderElement ?? container);
                }
                else if (!selected && container == SelectedContainer)
                {
                    // Unselect the old value
                    SelectedContainer.UpdateContainsSelection(false);
                    SelectedContainer     = null;
                    SelectedItem          = null;
                    SelectedValue         = null;
                    oldValue              = itemOrContainer;
                    raiseSelectionChanged = true;
                }

                container.IsSelected = selected;
            }
            finally
            {
                // Finish changing the selection
                IsSelectionChangeActive = false;
            }

            // Notify when the selection changes
            if (raiseSelectionChanged)
            {
                if (SelectedContainer != null && AutomationPeer.ListenerExists(AutomationEvents.SelectionItemPatternOnElementSelected))
                {
                    AutomationPeer peer = FrameworkElementAutomationPeer.CreatePeerForElement(SelectedContainer);
                    if (peer != null)
                    {
                        peer.RaiseAutomationEvent(AutomationEvents.SelectionItemPatternOnElementSelected);
                    }
                }
                if (element != null && AutomationPeer.ListenerExists(AutomationEvents.SelectionItemPatternOnElementRemovedFromSelection))
                {
                    AutomationPeer peer = FrameworkElementAutomationPeer.CreatePeerForElement(element);
                    if (peer != null)
                    {
                        peer.RaiseAutomationEvent(AutomationEvents.SelectionItemPatternOnElementRemovedFromSelection);
                    }
                }

                OnSelectedItemChanged(new RoutedPropertyChangedEventArgs <object>(oldValue, newValue));
            }
        }
示例#6
0
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="T:System.Windows.Controls.HeaderedItemsControl" /> class.
 /// </summary>
 public HeaderedItemsControl()
 {
     DefaultStyleKey    = typeof(HeaderedItemsControl);
     ItemsControlHelper = new ItemsControlHelper(this);
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="Accordion"/> class.
        /// </summary>
        public Accordion()
        {
#if SILVERLIGHT
            DefaultStyleKey = typeof(Accordion);
#endif
            ItemsControlHelper = new ItemsControlHelper(this);

            ObservableCollection<object> items = new ObservableCollection<object>();
            ObservableCollection<int> indices = new ObservableCollection<int>();

            SelectedItems = items;
            SelectedIndices = indices;

            items.CollectionChanged += OnSelectedItemsCollectionChanged;
            indices.CollectionChanged += OnSelectedIndicesCollectionChanged;

            _scheduledActions = new List<AccordionItem>();
            SizeChanged += OnAccordionSizeChanged;
            Interaction = new InteractionHelper(this);
        }
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="T:System.Windows.Controls.TreeView" /> class.
 /// </summary>
 public TreeView()
 {
     DefaultStyleKey = typeof(TreeView);
     ItemsControlHelper = new ItemsControlHelper(this);
     Interaction = new InteractionHelper(this);
 }
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="T:System.Windows.Controls.HeaderedItemsControl" /> class.
 /// </summary>
 public HeaderedItemsControl()
 {
     DefaultStyleKey = typeof(HeaderedItemsControl);
     ItemsControlHelper = new ItemsControlHelper(this);
 }
示例#10
0
        /// <summary>
        /// Applies control template to the items control.
        /// </summary>
        public override void OnApplyTemplate()
        {
            ItemsControlHelper.OnApplyTemplate();
            base.OnApplyTemplate();
#endif
        }
示例#11
0
 /// <summary>
 /// ItemContainerStyleProperty property changed handler.
 /// </summary>
 /// <param name="newValue">New value.</param>
 protected virtual void OnItemContainerStyleChanged(Style newValue)
 {
     ItemsControlHelper.UpdateItemContainerStyle(newValue);
 }