WPF:如何使画布自动调整大小?

时间:2009-05-12 23:09:01

标签: .net wpf resize wpf-controls scrollviewer

我希望我的Canvas自动调整大小到其项目的大小,以便ScrollViewer滚动条具有正确的范围。这可以在XAML中完成吗?

<ScrollViewer HorizontalScrollBarVisibility="Auto" x:Name="_scrollViewer">
    <Grid x:Name ="_canvasGrid" Background="Yellow">
        <Canvas x:Name="_canvas" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Green"></Canvas>
        <Line IsHitTestVisible="False" .../>
    </Grid>
</ScrollViewer>

在上面的代码中,画布的大小始终为0,但它不会剪切其子元素。

12 个答案:

答案 0 :(得分:48)

这是不可能的(请参阅下面的MSDN摘录)。但是,如果您想要滚动条和自动调整大小,请考虑使用网格,并使用Margin属性将项目放置在此Grid上。网格将告诉ScrollViewer他想要多大是的,你会得到滚动条..画布将始终告诉ScrollViewer他不需要任何大小.. :))

Grid让您享受这两个世界 - 只要您将所有元素放入单个单元格中,您就可以获得:任意定位和自动调整大小。一般来说,最好记住大多数面板控件(DockPanel,StackPanel等)都可以通过Grid控件实现。

来自MSDN

  

Canvas是唯一没有固有布局特征的面板元素。 Canvas的默认高度和宽度属性为零,除非它是自动调整其子元素大小的元素的子元素。画布的子元素永远不会调整大小,它们只是定位在指定的坐标处。这为不需要或不需要固有尺寸限制或对准的情况提供了灵活性。对于您希望子内容自动调整大小并对齐的情况,通常最好使用Grid元素。

希望这有帮助

答案 1 :(得分:36)

我只是在这里复制illef的答案,但在回答PilotBob时,你只需定义一个像这样的画布对象

public class CanvasAutoSize : Canvas
{
    protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint)
    {
        base.MeasureOverride(constraint);
        double width = base
            .InternalChildren
            .OfType<UIElement>()
            .Max(i => i.DesiredSize.Width + (double)i.GetValue(Canvas.LeftProperty));

        double height = base
            .InternalChildren
            .OfType<UIElement>()
            .Max(i => i.DesiredSize.Height + (double)i.GetValue(Canvas.TopProperty));

        return new Size(width, height);
    }
}

然后在XAML中使用CanvasAutoSize。

            <local:CanvasAutoSize VerticalAlignment="Top" HorizontalAlignment="Left"></local:CanvasAutoSize>

我更喜欢上面介绍的解决方案,它使用网格,因为它通过附加属性工作,只需要在元素上设置较少的属性。

答案 2 :(得分:9)

我认为您可以通过覆盖CanvasMeasureOverride方法来调整ArrangeOverride的大小。

这项工作并不困难。

你可以看到这篇文章。 http://illef.tistory.com/entry/Canvas-supports-ScrollViewer

我希望这会对你有所帮助。

谢谢。

答案 3 :(得分:6)

我看到你有一个可行的解决方案,但我想我会分享。

<Canvas x:Name="topCanvas">
    <Grid x:Name="topGrid" Width="{Binding ElementName=topCanvas, Path=ActualWidth}" Height="{Binding ElementName=topCanvas, Path=ActualHeight}">
        ...Content...
    </Grid>
</Canvas>

上述技术允许您在画布中嵌套网格并进行动态调整大小。进一步使用尺寸装订可以将动态材料与静态材料混合,执行分层等。有太多可能提及,有些比其他更难。例如,我使用该方法来模拟从一个网格位置移动到另一个网格位置的动画内容 - 在动画的完成事件中进行实际放置。祝你好运。

答案 4 :(得分:6)

基本上它需要完全重写Canvas。之前提出的覆盖MeasureOverride的解决方案失败,因为默认的Canvas.Left / .Top&amp; c属性使Arrangment无效,但是还需要使度量无效。 (第一次获得正确的大小,但如果在初始布局后移动元素,则大小不会改变。)

Grid解决方案或多或少是合理的,但为了获得x-y位移而绑定到Margins会对其他代码造成严重破坏(MVVM中的特殊情况)。我在网格视图解决方案上挣扎了一段时间,但View / ViewModel交互和滚动行为的复杂性最终促使我这样做。这很简单,也很重要,Just Works。

重新实现ArrangeOverride和MeasureOverride并不复杂。并且你必须在其他地方编写至少与处理网格/边距愚蠢相同的代码。所以你有。

这是一个更完整的解决方案。非零保证金行为未经测试。如果你需要除Left和Top之外的任何东西,那么这至少提供了一个起点。

警告:您必须使用AutoResizeCanvas.Left和AutoResizeCanvas.Top附加属性而不是Canvas.Left和Canvas.Top。剩余的Canvas属性尚未实现。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mu.Controls
{
    public class AutoResizeCanvas : Panel
    {



        public static double GetLeft(DependencyObject obj)
        {
            return (double)obj.GetValue(LeftProperty);
        }

        public static void SetLeft(DependencyObject obj, double value)
        {
            obj.SetValue(LeftProperty, value);
        }

        public static readonly DependencyProperty LeftProperty =
            DependencyProperty.RegisterAttached("Left", typeof(double),
            typeof(AutoResizeCanvas), 
            new FrameworkPropertyMetadata(0.0, OnLayoutParameterChanged));

        private static void OnLayoutParameterChanged(
                DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // invalidate the measure of the enclosing AutoResizeCanvas.
            while (d != null)
            {
                AutoResizeCanvas canvas = d as AutoResizeCanvas;
                if (canvas != null)
                {
                    canvas.InvalidateMeasure();
                    return;
                }
                d = VisualTreeHelper.GetParent(d);
            }
        }




        public static double GetTop(DependencyObject obj)
        {
            return (double)obj.GetValue(TopProperty);
        }

        public static void SetTop(DependencyObject obj, double value)
        {
            obj.SetValue(TopProperty, value);
        }

        public static readonly DependencyProperty TopProperty =
            DependencyProperty.RegisterAttached("Top", 
                typeof(double), typeof(AutoResizeCanvas),
                new FrameworkPropertyMetadata(0.0, OnLayoutParameterChanged));





        protected override Size MeasureOverride(Size constraint)
        {
            Size availableSize = new Size(double.MaxValue, double.MaxValue);
            double requestedWidth = MinimumWidth;
            double requestedHeight = MinimumHeight;
            foreach (var child in base.InternalChildren)
            {
                FrameworkElement el = child as FrameworkElement;

                if (el != null)
                {
                    el.Measure(availableSize);
                    Rect bounds, margin;
                    GetRequestedBounds(el,out bounds, out margin);

                    requestedWidth = Math.Max(requestedWidth, margin.Right);
                    requestedHeight = Math.Max(requestedHeight, margin.Bottom);
                }
            }
            return new Size(requestedWidth, requestedHeight);
        }
        private void GetRequestedBounds(
                            FrameworkElement el, 
                            out Rect bounds, out Rect marginBounds
                            )
        {
            double left = 0, top = 0;
            Thickness margin = new Thickness();
            DependencyObject content = el;
            if (el is ContentPresenter)
            {
                content = VisualTreeHelper.GetChild(el, 0);
            }
            if (content != null)
            {
                left = AutoResizeCanvas.GetLeft(content);
                top = AutoResizeCanvas.GetTop(content);
                if (content is FrameworkElement)
                {
                    margin = ((FrameworkElement)content).Margin;
                }
            }
            if (double.IsNaN(left)) left = 0;
            if (double.IsNaN(top)) top = 0;
            Size size = el.DesiredSize;
            bounds = new Rect(left + margin.Left, top + margin.Top, size.Width, size.Height);
            marginBounds = new Rect(left, top, size.Width + margin.Left + margin.Right, size.Height + margin.Top + margin.Bottom);
        }


        protected override Size ArrangeOverride(Size arrangeSize)
        {
            Size availableSize = new Size(double.MaxValue, double.MaxValue);
            double requestedWidth = MinimumWidth;
            double requestedHeight = MinimumHeight;
            foreach (var child in base.InternalChildren)
            {
                FrameworkElement el = child as FrameworkElement;

                if (el != null)
                {
                    Rect bounds, marginBounds;
                    GetRequestedBounds(el, out bounds, out marginBounds);

                    requestedWidth = Math.Max(marginBounds.Right, requestedWidth);
                    requestedHeight = Math.Max(marginBounds.Bottom, requestedHeight);
                    el.Arrange(bounds);
                }
            }
            return new Size(requestedWidth, requestedHeight);
        }

        public double MinimumWidth
        {
            get { return (double)GetValue(MinimumWidthProperty); }
            set { SetValue(MinimumWidthProperty, value); }
        }

        public static readonly DependencyProperty MinimumWidthProperty =
            DependencyProperty.Register("MinimumWidth", typeof(double), typeof(AutoResizeCanvas), 
            new FrameworkPropertyMetadata(300.0,FrameworkPropertyMetadataOptions.AffectsMeasure));



        public double MinimumHeight
        {
            get { return (double)GetValue(MinimumHeightProperty); }
            set { SetValue(MinimumHeightProperty, value); }
        }

        public static readonly DependencyProperty MinimumHeightProperty =
            DependencyProperty.Register("MinimumHeight", typeof(double), typeof(AutoResizeCanvas), 
            new FrameworkPropertyMetadata(200.0,FrameworkPropertyMetadataOptions.AffectsMeasure));



    }


}

答案 5 :(得分:2)

将高度/宽度绑定到画布中控件的实际大小为我工作:

        <ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
            <Canvas Height="{Binding ElementName=myListBox, Path=ActualHeight}"
                    Width="{Binding ElementName=myListBox, Path=ActualWidth}">
                <ListBox x:Name="myListBox" />
            </Canvas>
        </ScrollViewer>

答案 6 :(得分:1)

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    autoSizeCanvas(canvas1);
}

void autoSizeCanvas(Canvas canv)
{
    int height = canv.Height;
    int width = canv.Width;
    foreach (UIElement ctrl in canv.Children)
    {
        bool nullTop = ctrl.GetValue(Canvas.TopProperty) == null || Double.IsNaN(Convert.ToDouble(ctrl.GetValue(Canvas.TopProperty))),
                nullLeft = ctrl.GetValue(Canvas.LeftProperty) == null || Double.IsNaN(Convert.ToDouble(ctrl.GetValue(Canvas.LeftProperty)));
        int curControlMaxY = (nullTop ? 0 : Convert.ToInt32(ctrl.GetValue(Canvas.TopProperty))) +
            Convert.ToInt32(ctrl.GetValue(Canvas.ActualHeightProperty)
            ),
            curControlMaxX = (nullLeft ? 0 : Convert.ToInt32(ctrl.GetValue(Canvas.LeftProperty))) +
            Convert.ToInt32(ctrl.GetValue(Canvas.ActualWidthProperty)
            );
        height = height < curControlMaxY ? curControlMaxY : height;
        width = width < curControlMaxX ? curControlMaxX : width;
    }
    canv.Height = height;
    canv.Width = width;
}

在函数中,我试图找到最大X位置和Y位置,画布中的控件可以驻留在这里。

仅在Loaded事件或更高版本中使用该函数,而不是在构造函数中使用。必须在装载前测量窗口..

答案 7 :(得分:1)

作为对@ MikeKulls的回答的改进,这里有一个版本,当画布中没有UI元素或没有Canvas.Top或Canvas.Left的UI元素时,它不会抛出异常属性:

public class AutoResizedCanvas : Canvas
{
    protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint)
    {
        base.MeasureOverride(constraint);
        double width = base
            .InternalChildren
            .OfType<UIElement>()
            .Where(i => i.GetValue(Canvas.LeftProperty) != null)
            .Max(i => i.DesiredSize.Width + (double)i.GetValue(Canvas.LeftProperty));

        if (Double.IsNaN(width))
        {
            width = 0;
        }

        double height = base
            .InternalChildren
            .OfType<UIElement>()
            .Where(i => i.GetValue(Canvas.TopProperty) != null)
            .Max(i => i.DesiredSize.Height + (double)i.GetValue(Canvas.TopProperty));

        if (Double.IsNaN(height))
        {
            height = 0;
        }

        return new Size(width, height);
    }
}

答案 8 :(得分:0)

我也遇到过这个问题,我的问题是,由于覆盖了MeasureOverride函数,当Canvas调整大小时,网格没有自动调整大小。

我的问题: WPF MeasureOverride loop

答案 9 :(得分:0)

我能够通过向控件添加一个新的大小更改事件来实现您正在寻找的结果,该控件包含导致画布增长的数据。在画布到达滚动查看器的范围后,它将导致滚动条出现。我刚刚将以下lambda表达式分配给控件的size改变事件:

text2.SizeChanged += (s, e) => { DrawingCanvas.Height = e.NewSize.Height; 
                                 DrawingCanvas.Width = e.NewSize.Width; };

答案 10 :(得分:0)

对我有用的是: 就像他们问题中原始海报的示例一样,我将画布嵌套在网格中。网格在scrollviewer中。我没有尝试更改画布大小,而是更改了网格大小(在我的情况下为高和宽),并且画布遵循网格大小减去任何边距。我以编程方式设置网格大小,尽管我认为绑定也可以工作。我也以编程方式获得了所需的网格大小。

答案 11 :(得分:-2)

<viewbox>
    <canvas>
        <uielements /> 
    </canvas>
</viewbox>
相关问题