Exemple #1
0
            protected override sw.Size MeasureOverride(sw.Size constraint)
            {
                // once loaded, act as normal
                if (IsLoaded)
                {
                    return(base.MeasureOverride(constraint));
                }

                // not loaded yet, let's calculate size based on all tabs
                var size         = new sw.Size(0, 0);
                var selectedSize = new sw.Size(0, 0);
                var selected     = SelectedItem as swc.TabItem;

                foreach (var tab in Items.Cast <swc.TabItem>())
                {
                    var tabContent = tab.Content as sw.FrameworkElement;
                    if (tabContent == null)
                    {
                        continue;
                    }
                    tabContent.Measure(constraint);
                    var tabSize = tabContent.DesiredSize;
                    if (tab == selected)
                    {
                        selectedSize = tabSize;
                    }
                    size = size.Max(tabSize);
                }
                var baseSize = base.MeasureOverride(constraint);
                // calculate size of the border around the content based on selected tab's content size
                var borderSize = baseSize.Subtract(selectedSize);

                // return max height with border
                return(size.Add(borderSize));
            }
Exemple #2
0
        public override sw.Size GetPreferredSize(sw.Size constraint)
        {
            if (columnScale == null || columnScale.Length == 0 || rowScale == null || rowScale.Length == 0)
            {
                return(ContainerControl.GetMinSize().Add(ContainerControl.Margin.Size()));
            }

            var margin = ContainerControl.Margin.Size();
            var size   = PreferredSize;

            if (double.IsNaN(size.Width) || double.IsNaN(size.Height))
            {
                var padding         = border.Padding.Size();
                var widths          = new double[columnScale.Length];
                var heights         = new double[rowScale.Length];
                var scaledCells     = new Size();
                var childConstraint = constraint.Subtract(padding);
                childConstraint = childConstraint.Subtract(margin);

                // check non-scaled rows/columns first with preferred size
                for (int y = 0; y < rowScale.Length; y++)
                {
                    var cellConstraint = childConstraint;
                    var hasRowScale    = IsRowScaled(y);
                    if (hasRowScale)
                    {
                        scaledCells.Height++;
                    }
                    else
                    {
                        cellConstraint.Height = double.PositiveInfinity;
                    }
                    for (int x = 0; x < widths.Length; x++)
                    {
                        var hasColScale = IsColumnScaled(x);
                        if (hasColScale && y == 0)
                        {
                            scaledCells.Width++;
                        }
                        if (!hasColScale)
                        {
                            cellConstraint.Width = double.PositiveInfinity;
                        }
                        if (!hasColScale || !hasRowScale)
                        {
                            var control      = controls[x, y];
                            var childControl = control.GetWpfFrameworkElement();
                            if (childControl != null && control.Visible)
                            {
                                var preferredSize = childControl.GetPreferredSize(cellConstraint);
                                if (!hasColScale)
                                {
                                    widths[x] = Math.Max(widths[x], preferredSize.Width);
                                }
                                if (!hasRowScale)
                                {
                                    heights[y] = Math.Max(heights[y], preferredSize.Height);
                                }
                            }
                            else
                            {
                                var cellMargins = GetMargins(x, y);
                                widths[x]  = Math.Max(widths[x], cellMargins.Horizontal());
                                heights[y] = Math.Max(heights[y], cellMargins.Vertical());
                            }
                        }
                    }
                }

                // find remaning size for each cell
                childConstraint         = childConstraint.Subtract(new sw.Size(widths.Sum(), heights.Sum()));
                childConstraint.Width  /= scaledCells.Width;
                childConstraint.Height /= scaledCells.Height;

                // check scaled rows/columns based on remaining size
                for (int y = 0; y < rowScale.Length; y++)
                {
                    var cellConstraint = childConstraint;
                    var hasRowScale    = IsRowScaled(y);
                    if (!hasRowScale)
                    {
                        cellConstraint.Height = double.PositiveInfinity;
                    }
                    for (int x = 0; x < widths.Length; x++)
                    {
                        var hasColScale = IsColumnScaled(x);
                        if (!hasColScale)
                        {
                            cellConstraint.Width = double.PositiveInfinity;
                        }
                        if (hasColScale || hasRowScale)
                        {
                            var control      = controls[x, y];
                            var childControl = control.GetWpfFrameworkElement();
                            if (childControl != null && control.Visible)
                            {
                                var preferredSize = childControl.GetPreferredSize(cellConstraint);
                                widths[x]  = Math.Max(widths[x], preferredSize.Width);
                                heights[y] = Math.Max(heights[y], preferredSize.Height);
                            }
                            else
                            {
                                var cellMargins = GetMargins(x, y);
                                widths[x]  = Math.Max(widths[x], cellMargins.Horizontal());
                                heights[y] = Math.Max(heights[y], cellMargins.Vertical());
                            }
                        }
                    }
                }
                var preferredContentSize = new sw.Size(widths.Sum(), heights.Sum());
                preferredContentSize = preferredContentSize.Add(padding);

                size = size.IfNaN(preferredContentSize);
            }

            size = size.Max(ContainerControl.GetMinSize());
            size = size.Add(margin);
            //System.Diagnostics.Debug.WriteLine("Size: {0}, actual: {1}", size, ContainerControl.GetSize());
            return(size);
        }