这是根据 XAML Lover 和 Marat Khasanov 的建议解决我的问题的实现。 TestCustomControl 将 2 个文本框彼此对角放置。右上角的使用尽可能多的宽度和尽可能高的高度。左下方的那个使用尽可能多的高度和尽可能多的宽度。
+------------+--------+
| | |
| |TextBox1|
| | |
+------------+--------+
| TextBox2 | |
+------------+--------+
这种排列方式对于 Grid 来说是个问题,因为要测量一个 TextBox 的大小,它需要知道另一个 TextBox 的大小,而这是不可能的。 Grid 通过使用无限空间测量第一个 TextBox,然后使用第一个文本框的大小测量另一个文本框,然后使用第二个文本框的大小再次测量第一个文本框来解决这个难题。这会导致各种问题,而 TestCustomControl 完全可以避免这些问题。
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace TestControl {
public class TestCustomControl: Control {
VisualCollection visualCollection;
Rectangle RectangleToLeft;
TextBox TextBoxTopRight;
TextBox TextBoxBottomLeft;
Rectangle RectangleBottomRight;
public TestCustomControl() {
RectangleToLeft = new Rectangle { Fill = Brushes.LightYellow };
TextBoxTopRight = new TextBox {Text = "TR", FontSize = 22, HorizontalAlignment=HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Stretch };
TextBoxBottomLeft = new TextBox {Text = "BL", FontSize = 22, HorizontalAlignment=HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Center };
RectangleBottomRight = new Rectangle {Fill = Brushes.LightBlue };
//new UIElementCollection(this, logicalParent);
visualCollection = new VisualCollection(this);
visualCollection.Add(RectangleToLeft);
visualCollection.Add(TextBoxTopRight);
visualCollection.Add(TextBoxBottomLeft);
visualCollection.Add(RectangleBottomRight);
}
protected override int VisualChildrenCount {
get {
return visualCollection.Count;
}
}
protected override System.Windows.Media.Visual GetVisualChild(int index) {
return visualCollection[index];
}
protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint) {
TextBoxTopRight.Measure(constraint);
TextBoxBottomLeft.Measure(constraint);
Size returnedSize = constraint;
if (double.IsInfinity(constraint.Height)) {
returnedSize.Height = TextBoxTopRight.DesiredSize.Height + TextBoxBottomLeft.DesiredSize.Height;
}
if (double.IsInfinity(constraint.Width)) {
returnedSize.Width = TextBoxTopRight.DesiredSize.Width + TextBoxBottomLeft.DesiredSize.Width;
}
return constraint;
}
protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeBounds) {
double remainingWidth = Math.Max(0, arrangeBounds.Width - TextBoxTopRight.DesiredSize.Width);
double remainingHeight = Math.Max(0, arrangeBounds.Height - TextBoxBottomLeft.DesiredSize.Height);
RectangleToLeft.Arrange(new Rect(0, 0, remainingWidth, remainingHeight));
TextBoxTopRight.Arrange(new Rect(remainingWidth, 0, TextBoxTopRight.DesiredSize.Width, remainingHeight));
TextBoxBottomLeft.Arrange(new Rect(0, remainingHeight, remainingWidth, TextBoxBottomLeft.DesiredSize.Height));
RectangleBottomRight.Arrange(new Rect(remainingWidth, remainingHeight, TextBoxTopRight.DesiredSize.Width, TextBoxTopRight.DesiredSize.Height));
return arrangeBounds;
}
protected override Geometry GetLayoutClip(Size layoutSlotSize) {
if (ClipToBounds)
return new RectangleGeometry(new Rect(RenderSize));
else
return null;
}
}
}