我已经在这个问题上苦苦思索了一段时间。
我有一个自定义控件,继承自ContentControl - 它完美地工作,除了它无法根据其父控件的大小进行自适应调整,无论是声明式还是编程式。
自定义控件是在(最终)内容呈现器中进行父级控制,并且该呈现器已正确调整大小,但我的控件就是不会自动调整大小以适应。
(sample.MyControl) - 400x267
- (System.Windows.Controls.ContentPresenter) - 979x569
即使我在正确的时机明确设置了宽度和高度,大小也不会“固定”。
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));
上面的代码会输出:
MyControl 36022757: Size is 400x267 (Stretch/Stretch)
Parent is 979x569 (Stretch/Stretch)
MyControl 36022757: New size is 400x267
为什么!哦,天哪为什么!
有人有任何想法吗?