ContentControl调整大小的问题

Ram*_*ses 4 silverlight resize

我现在已经用这一块撞墙了.

我有一个派生自ContentControl的自定义控件 - 它完美地工作除了它不会调整大小以适应其父级,无论是以声明方式还是以编程方式.

自定义控件是(最终)内容演示者的父级,并且大小正确,但我的控件不会自行调整大小.

  (sample.MyControl) - 400x267
-  (System.Windows.Controls.ContentPresenter) - 979x569
Run Code Online (Sandbox Code Playgroud)

即使我明确设置了宽度和高度(在合适的时刻),尺寸也不会"粘住"

        Debug.WriteLine(string.Format("MyControl {0}: Size is {1}x{2} ({3}/{4})",
            GetHashCode(), this.ActualWidth, this.ActualHeight,
            this.HorizontalAlignment, this.VerticalAlignment));

        Debug.WriteLine(string.Format("Parent is {0}x{1} ({2}/{3})",
                                      parent.ActualWidth, parent.ActualHeight, 
                                      parent.HorizontalAlignment, parent.VerticalAlignment));

        this.Width = parent.ActualWidth;
        this.Height = parent.ActualHeight;

        Debug.WriteLine(string.Format("MyControl {0}: New size is {1}x{2}",
            GetHashCode(), this.ActualWidth, this.ActualHeight));
Run Code Online (Sandbox Code Playgroud)

上面的代码给出:

MyControl 36022757: Size is 400x267 (Stretch/Stretch)
Parent is 979x569 (Stretch/Stretch)
MyControl 36022757: New size is 400x267
Run Code Online (Sandbox Code Playgroud)

为什么!哦天哪,为什么!

有人有任何想法吗?

Ale*_* K. 17

您只需添加HorizontalContentAlignment="Stretch"VerticalContentAlignment="Stretch"控制:

<ContentControl HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
    ...
</ContentControl>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,Silverlight Spy工具可以帮助调试运行时的布局.

尝试在此工具中运行您的应用程序,您将能够查看所有xaml元素,甚至可以在运行时更改某些属性.