Exemplo n.º 1
0
        /// <summary>
        /// Will update the <see cref="CurrentItem"/> property. This method will be called when the
        /// current item might have changed.
        /// </summary>
        protected void UpdateCurrentItem()
        {
            Screen           screen         = Screen;
            FrameworkElement focusedElement = screen == null ? null : screen.FocusedElement;

            if (_lastFocusedElement == focusedElement)
            {
                return;
            }
            _lastFocusedElement = focusedElement;
            object lastCurrentItem = CurrentItem;
            object newCurrentItem  = null;

            if (_itemsHostPanel != null && CheckFocusInScope(focusedElement))
            {
                Visual element = focusedElement;
                while (element != null && element.VisualParent != _itemsHostPanel)
                {
                    element = element.VisualParent;
                }
                newCurrentItem = element == null ? null : element.Context;
                ISelectableItemContainer container = element as ISelectableItemContainer;
                if (container != null)
                {
                    container.Selected = true; // Triggers an update of our _lastSelectedItem
                }
            }
            if (newCurrentItem != lastCurrentItem)
            {
                CurrentItem = newCurrentItem;
                FireSelectionChanged(newCurrentItem);
            }
        }
Exemplo n.º 2
0
        public override void DeepCopy(IDeepCopyable source, ICopyManager copyManager)
        {
            _preventItemsPreparation = true;
            Detach();
            base.DeepCopy(source, copyManager);
            ItemsControl c = (ItemsControl)source;

            ItemsSource = copyManager.GetCopy(c.ItemsSource);
            _items.Clear();
            foreach (object item in c.Items)
            {
                _items.Add(copyManager.GetCopy(item));
            }
            ItemContainerStyle        = copyManager.GetCopy(c.ItemContainerStyle);
            ItemTemplate              = copyManager.GetCopy(c.ItemTemplate);
            GroupHeaderContainerStyle = copyManager.GetCopy(c.GroupHeaderContainerStyle);
            GroupHeaderTemplate       = copyManager.GetCopy(c.GroupHeaderTemplate);
            ItemsPanel         = copyManager.GetCopy(c.ItemsPanel);
            DataStringProvider = copyManager.GetCopy(c.DataStringProvider);
            RestoreFocus       = c.RestoreFocus;
            _lastSelectedItem  = copyManager.GetCopy(c._lastSelectedItem);
            Attach();
            _preventItemsPreparation   = false;
            copyManager.CopyCompleted += manager => PrepareItems(false); // During the deep copy process, our referenced objects are not initialized yet so defer preparation of items to the end of the copy process
        }
Exemplo n.º 3
0
        public void UpdateSelectedItem(ISelectableItemContainer container)
        {
            bool isSelected = container != null && container.Selected;

            if (ReferenceEquals(_lastSelectedItem, container))
            { // Our selected container
                if (!isSelected)
                {
                    _lastSelectedItem = null;
                }
                return;
            }
            // Not our selected container
            if (!isSelected)
            {
                return;
            }
            if (_lastSelectedItem != null)
            {
                _lastSelectedItem.Selected = false;
            }
            _lastSelectedItem = container;
        }
Exemplo n.º 4
0
 protected void UpdatePreparedItems()
 {
   bool doSetItems;
   bool doSetChildren;
   ItemCollection preparedItems;
   ItemCollection preparedChildren;
   lock (_renderLock)
   {
     if (!_setItems && !_setChildren)
       return;
     doSetItems = _setItems;
     _setItems = false;
     doSetChildren = _setChildren;
     _setChildren = false;
     preparedItems = _preparedItems;
     _preparedItems = null;
     preparedChildren = _preparedChildren;
     _preparedChildren = null;
   }
   Panel itemsHostPanel = _itemsHostPanel;
   if (doSetChildren)
   {
     FrameworkElementCollection children = itemsHostPanel.Children;
     lock (children.SyncRoot)
     {
       children.Clear(false);
       if (preparedChildren != null)
       {
         IList<FrameworkElement> tempItems = new List<FrameworkElement>(preparedChildren.Count);
         foreach (object item in preparedChildren.ExtractElements())
         {
           FrameworkElement fe = item as FrameworkElement;
           if (fe == null)
           {
             MPF.TryCleanupAndDispose(item);
             continue;
           }
           ISelectableItemContainer sic = item as ISelectableItemContainer;
           if (sic != null && sic.Selected)
             _lastSelectedItem = sic;
           tempItems.Add(fe);
         }
         children.AddAll(tempItems);
         preparedChildren.Dispose();
       }
     }
   }
   if (doSetItems)
   {
     _items.Clear();
     if (preparedItems != null)
     {
       _items.AddAll(preparedItems.ExtractElements());
       preparedItems.Dispose();
     }
   }
   VirtualizingStackPanel vsp = itemsHostPanel as VirtualizingStackPanel;
   if (vsp == null)
     IsEmpty = itemsHostPanel.Children.Count == 0;
   // else IsEmpty has been updated by PrepareItemsOverride
 }
Exemplo n.º 5
0
 public void UpdateSelectedItem(ISelectableItemContainer container)
 {
   bool isSelected = container != null && container.Selected;
   if (ReferenceEquals(_lastSelectedItem, container))
   { // Our selected container
     if (!isSelected)
       _lastSelectedItem = null;
     return;
   }
   // Not our selected container
   if (!isSelected)
     return;
   if (_lastSelectedItem != null)
     _lastSelectedItem.Selected = false;
   _lastSelectedItem = container;
 }
Exemplo n.º 6
0
 public override void DeepCopy(IDeepCopyable source, ICopyManager copyManager)
 {
   _preventItemsPreparation = true;
   Detach();
   base.DeepCopy(source, copyManager);
   ItemsControl c = (ItemsControl) source;
   ItemsSource = copyManager.GetCopy(c.ItemsSource);
   _items.Clear();
   foreach (object item in c.Items)
     _items.Add(copyManager.GetCopy(item));
   ItemContainerStyle = copyManager.GetCopy(c.ItemContainerStyle);
   SelectionChanged = copyManager.GetCopy(c.SelectionChanged);
   ItemTemplate = copyManager.GetCopy(c.ItemTemplate);
   ItemsPanel = copyManager.GetCopy(c.ItemsPanel);
   DataStringProvider = copyManager.GetCopy(c.DataStringProvider);
   _lastSelectedItem = copyManager.GetCopy(c._lastSelectedItem);
   Attach();
   _preventItemsPreparation = false;
   copyManager.CopyCompleted += manager => PrepareItems(false); // During the deep copy process, our referenced objects are not initialized yet so defer preparation of items to the end of the copy process
 }
Exemplo n.º 7
0
        protected void UpdatePreparedItems()
        {
            bool           doSetItems;
            bool           doSetChildren;
            ItemCollection preparedItems;
            ItemCollection preparedChildren;

            lock (_renderLock)
            {
                if (!_setItems && !_setChildren)
                {
                    return;
                }
                doSetItems        = _setItems;
                _setItems         = false;
                doSetChildren     = _setChildren;
                _setChildren      = false;
                preparedItems     = _preparedItems;
                _preparedItems    = null;
                preparedChildren  = _preparedChildren;
                _preparedChildren = null;
            }
            Panel itemsHostPanel = _itemsHostPanel;

            if (doSetChildren)
            {
                FrameworkElementCollection children = itemsHostPanel.Children;
                lock (children.SyncRoot)
                {
                    children.Clear(false);
                    if (preparedChildren != null)
                    {
                        IList <FrameworkElement> tempItems = new List <FrameworkElement>(preparedChildren.Count);
                        foreach (object item in preparedChildren.ExtractElements())
                        {
                            FrameworkElement fe = item as FrameworkElement;
                            if (fe == null)
                            {
                                MPF.TryCleanupAndDispose(item);
                                continue;
                            }
                            ISelectableItemContainer sic = item as ISelectableItemContainer;
                            if (sic != null && sic.Selected)
                            {
                                _lastSelectedItem = sic;
                            }
                            tempItems.Add(fe);
                        }
                        children.AddAll(tempItems);
                        preparedChildren.Dispose();
                    }
                }
            }
            if (doSetItems)
            {
                _items.Clear();
                if (preparedItems != null)
                {
                    _items.AddAll(preparedItems.ExtractElements());
                    preparedItems.Dispose();
                }
            }
            VirtualizingStackPanel vsp          = itemsHostPanel as VirtualizingStackPanel;
            IItemProvider          itemProvider = vsp == null ? null : vsp.ItemProvider;

            IsEmpty = (itemProvider == null ? itemsHostPanel.Children.Count : itemProvider.NumItems) == 0;
        }