Exemplo n.º 1
0
        /// <summary>
        /// Called when the <see cref="Content"/> property changes.
        /// </summary>
        /// <param name="e">The event args.</param>
        private void ContentChanged(AvaloniaPropertyChangedEventArgs e)
        {
            _createdChild = false;

            if (((ILogical)this).IsAttachedToLogicalTree)
            {
                UpdateChild();
            }
            else if (Child != null)
            {
                VisualChildren.Remove(Child);
                LogicalChildren.Remove(Child);
                ((ISetInheritanceParent)Child).SetParent(Child.Parent);
                Child         = null;
                _dataTemplate = null;
            }

            InvalidateMeasure();
        }
Exemplo n.º 2
0
        protected override void OnDataContextEndUpdate()
        {
            base.OnDataContextEndUpdate();

            if (Content as ILogical != null)
            {
                LogicalChildren.Remove(Content as ILogical);
            }

            if (DataContext != null)
            {
                Content = ViewLocator.Build(DataContext);
            }

            if (Content as ILogical != null)
            {
                LogicalChildren.Add(Content as ILogical);
            }
        }
Exemplo n.º 3
0
        private void ThumbChanged(AvaloniaPropertyChangedEventArgs e)
        {
            var oldThumb = (Thumb)e.OldValue;
            var newThumb = (Thumb)e.NewValue;

            if (oldThumb != null)
            {
                oldThumb.DragDelta -= ThumbDragged;

                LogicalChildren.Remove(oldThumb);
                VisualChildren.Remove(oldThumb);
            }

            if (newThumb != null)
            {
                newThumb.DragDelta += ThumbDragged;
                LogicalChildren.Add(newThumb);
                VisualChildren.Add(newThumb);
            }
        }
Exemplo n.º 4
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;
        }
Exemplo n.º 5
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);
                }
            }
        }