예제 #1
0
        /// <summary>
        /// Creates the <see cref="Panel"/> when <see cref="ApplyTemplate"/> is called for the first
        /// time.
        /// </summary>
        protected virtual void CreatePanel()
        {
            Panel = ItemsPanel.Build();
            Panel.SetValue(TemplatedParentProperty, TemplatedParent);

            LogicalChildren.Clear();
            VisualChildren.Clear();
            LogicalChildren.Add(Panel);
            VisualChildren.Add(Panel);

            _createdPanel = true;

            INotifyCollectionChanged incc = Items as INotifyCollectionChanged;

            if (incc != null)
            {
                incc.CollectionChanged += ItemsCollectionChanged;
            }

            ItemsChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }
예제 #2
0
        /// <summary>
        /// Creates the <see cref="Panel"/> when <see cref="ApplyTemplate"/> is called for the first
        /// time.
        /// </summary>
        private void CreatePanel()
        {
            Panel = ItemsPanel.Build();
            Panel.SetValue(TemplatedParentProperty, TemplatedParent);

            LogicalChildren.Clear();
            VisualChildren.Clear();
            LogicalChildren.Add(Panel);
            VisualChildren.Add(Panel);

            _createdPanel = true;

            if (!IsHosted && _itemsSubscription == null && Items is INotifyCollectionChanged incc)
            {
                _itemsSubscription = incc.WeakSubscribe(ItemsCollectionChanged);
            }

            PanelCreated(Panel);

            ItemsChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }
예제 #3
0
파일: Panel.cs 프로젝트: yzq1979/Avalonia
        /// <summary>
        /// Called when the <see cref="Children"/> collection changes.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event args.</param>
        protected virtual void ChildrenChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            List <Control> controls;

            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Add:
                controls = e.NewItems.OfType <Control>().ToList();
                LogicalChildren.InsertRange(e.NewStartingIndex, controls);
                VisualChildren.InsertRange(e.NewStartingIndex, e.NewItems.OfType <Visual>());
                break;

            case NotifyCollectionChangedAction.Move:
                LogicalChildren.MoveRange(e.OldStartingIndex, e.OldItems.Count, e.NewStartingIndex);
                VisualChildren.MoveRange(e.OldStartingIndex, e.OldItems.Count, e.NewStartingIndex);
                break;

            case NotifyCollectionChangedAction.Remove:
                controls = e.OldItems.OfType <Control>().ToList();
                LogicalChildren.RemoveAll(controls);
                VisualChildren.RemoveAll(e.OldItems.OfType <Visual>());
                break;

            case NotifyCollectionChangedAction.Replace:
                for (var i = 0; i < e.OldItems.Count; ++i)
                {
                    var index = i + e.OldStartingIndex;
                    var child = (IControl)e.NewItems[i];
                    LogicalChildren[index] = child;
                    VisualChildren[index]  = child;
                }
                break;

            case NotifyCollectionChangedAction.Reset:
                throw new NotSupportedException();
            }

            InvalidateMeasure();
        }
예제 #4
0
        /// <summary>
        /// Creates the <see cref="Panel"/> when <see cref="ApplyTemplate"/> is called for the first
        /// time.
        /// </summary>
        private void CreatePanel()
        {
            Panel = ItemsPanel.Build();
            Panel.SetValue(TemplatedParentProperty, TemplatedParent);

            if (!Panel.IsSet(KeyboardNavigation.DirectionalNavigationProperty))
            {
                KeyboardNavigation.SetDirectionalNavigation(
                    (InputElement)Panel,
                    KeyboardNavigationMode.Contained);
            }

            LogicalChildren.Clear();
            VisualChildren.Clear();
            LogicalChildren.Add(Panel);
            VisualChildren.Add(Panel);

            KeyboardNavigation.SetTabNavigation(
                (InputElement)Panel,
                KeyboardNavigation.GetTabNavigation(this));
            _createdPanel = true;
            CreateItemsAndListenForChanges(Items);
        }
예제 #5
0
파일: Page.cs 프로젝트: tralivali1234/maui
        protected virtual void LayoutChildren(double x, double y, double width, double height)
        {
            var       area         = new Rectangle(x, y, width, height);
            Rectangle originalArea = area;

            if (_containerAreaSet)
            {
                area         = ContainerArea;
                area.X      += Padding.Left;
                area.Y      += Padding.Right;
                area.Width  -= Padding.HorizontalThickness;
                area.Height -= Padding.VerticalThickness;
                area.Width   = Math.Max(0, area.Width);
                area.Height  = Math.Max(0, area.Height);
            }

            List <Element> elements = LogicalChildren.ToList();

            foreach (Element element in elements)
            {
                var child = element as VisualElement;
                if (child == null)
                {
                    continue;
                }
                var page = child as Page;
                if (page != null && page.IgnoresContainerArea)
                {
                    Maui.Controls.Layout.LayoutChildIntoBoundingRegion(child, originalArea);
                }
                else
                {
                    Maui.Controls.Layout.LayoutChildIntoBoundingRegion(child, area);
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Updates the <see cref="Child"/> control based on the control's <see cref="Content"/>.
        /// </summary>
        /// <remarks>
        /// Usually the <see cref="Child"/> control is created automatically when
        /// <see cref="ApplyTemplate"/> is called; however for this to happen, the control needs to
        /// be attached to a logical tree (if the control is not attached to the logical tree, it
        /// is reasonable to expect that the DataTemplates needed for the child are not yet
        /// available). This method forces the <see cref="Child"/> control's creation at any point,
        /// and is particularly useful in unit tests.
        /// </remarks>
        public void UpdateChild()
        {
            var content  = Content;
            var oldChild = Child;
            var newChild = content as IControl;

            if (content != null && newChild == null)
            {
                // We have content and it isn't a control, so first try to recycle the existing
                // child control to display the new data by querying if the template that created
                // the child can recycle items and that it also matches the new data.
                if (oldChild != null &&
                    _dataTemplate != null &&
                    _dataTemplate.SupportsRecycling &&
                    _dataTemplate.Match(content))
                {
                    newChild = oldChild;
                }
                else
                {
                    // We couldn't recycle an existing control so find a data template for the data
                    // and use it to create a control.
                    _dataTemplate = this.FindDataTemplate(content, ContentTemplate) ?? FuncDataTemplate.Default;
                    newChild      = _dataTemplate.Build(content);

                    // Try to give the new control its own name scope.
                    var controlResult = newChild as Control;

                    if (controlResult != null)
                    {
                        NameScope.SetNameScope(controlResult, new NameScope());
                    }
                }
            }
            else
            {
                _dataTemplate = null;
            }

            // Remove the old child if we're not recycling it.
            if (oldChild != null && newChild != oldChild)
            {
                VisualChildren.Remove(oldChild);
            }

            // Set the DataContext if the data isn't a control.
            if (!(content is IControl))
            {
                DataContext = content;
            }

            // Update the Child.
            if (newChild == null)
            {
                Child = null;
            }
            else if (newChild != oldChild)
            {
                ((ISetInheritanceParent)newChild).SetParent(this);

                Child = newChild;

                if (oldChild?.Parent == this)
                {
                    LogicalChildren.Remove(oldChild);
                }

                if (newChild.Parent == null)
                {
                    var templatedLogicalParent = TemplatedParent as ILogical;

                    if (templatedLogicalParent != null)
                    {
                        ((ISetLogicalParent)newChild).SetParent(templatedLogicalParent);
                    }
                    else
                    {
                        LogicalChildren.Add(newChild);
                    }
                }

                VisualChildren.Add(newChild);
            }

            _createdChild = true;
        }
예제 #7
0
        internal bool IsTopMost(Window window)
        {
            int index = LogicalChildren.IndexOf(window);

            return(index >= 0 && index == LogicalChildren.Count - 1);
        }
예제 #8
0
        protected override Size ArrangeOverride(Size finalSize)
        {
            int firstRow = Cells.GetInitialRow();

            foreach (var cell in Cells.GetVisibleCells())
            {
                double width = AccumulatedColumnWidths[cell.Column.Index + 1] -
                               AccumulatedColumnWidths[cell.Column.Index] - GridControl.VerticalLinesThickness;
                cell.Arrange(new Rect(AccumulatedColumnWidths[cell.Column.Index],
                                      AccumulatedRowHeights[cell.Row - firstRow],
                                      width, cell.DesiredSize.Height));
            }

            UpdateGridLines();
            ScrollBar.Arrange(new Rect(finalSize.Width - ScrollBar.Width, 0.0, ScrollBar.Width, finalSize.Height));
            return(finalSize);

            void UpdateGridLines()
            {
                int lastAccumulatedColumnWidthsIndex = AccumulatedColumnWidths.Count - 1;

                while (ColumnLines.Count > lastAccumulatedColumnWidthsIndex)
                {
                    var columnLine = ColumnLines[ColumnLines.Count - 1];
                    ColumnLines.Remove(columnLine);
                    LogicalChildren.Remove(columnLine);
                    VisualChildren.Remove(columnLine);
                }
                while (lastAccumulatedColumnWidthsIndex > ColumnLines.Count)
                {
                    Line columnLine = new Line();
                    columnLine.Stroke          = GridControl.VerticalLinesBrush;
                    columnLine.StrokeThickness = GridControl.VerticalLinesThickness;
                    ColumnLines.Add(columnLine);
                    LogicalChildren.Add(columnLine);
                    VisualChildren.Add(columnLine);
                }

                int lastAccumulatedRowHeightsIndex = AccumulatedRowHeights.Count(rh => !Double.IsNaN(rh)) - 1;

                while (RowLines.Count > lastAccumulatedRowHeightsIndex)
                {
                    var rowLine = RowLines[RowLines.Count - 1];
                    RowLines.Remove(rowLine);
                    LogicalChildren.Remove(rowLine);
                    VisualChildren.Remove(rowLine);
                }

                while (lastAccumulatedRowHeightsIndex > RowLines.Count)
                {
                    Line rowLine = new Line();
                    rowLine.Stroke          = GridControl.HorizontalLinesBrush;
                    rowLine.StrokeThickness = GridControl.HorizontalLinesThickness;
                    RowLines.Add(rowLine);
                    LogicalChildren.Add(rowLine);
                    VisualChildren.Add(rowLine);
                }

                for (int i = 0; i < RowLines.Count; i++)
                {
                    double y = AccumulatedRowHeights[i + 1] - GridControl.HorizontalLinesThickness / 2.0;
                    RowLines[i].StartPoint = new Point(0.0, y);
                    RowLines[i].EndPoint   = new Point(AccumulatedColumnWidths[lastAccumulatedColumnWidthsIndex], y);
                }

                for (int i = 0; i < ColumnLines.Count; i++)
                {
                    double x = AccumulatedColumnWidths[i + 1] - GridControl.VerticalLinesThickness / 2.0;
                    ColumnLines[i].StartPoint = new Point(x, 0.0);
                    ColumnLines[i].EndPoint   = new Point(x, AccumulatedRowHeights[lastAccumulatedRowHeightsIndex]);
                }

                foreach (var line in RowLines.Concat(ColumnLines))
                {
                    line.InvalidateMeasure();
                    line.Measure(Size.Infinity);
                }
            }
        }