我有一个自定义控件,该控件在自定义面板(都是我的)中“托管”。
我的自定义面板有如下代码:
protected override Size MeasureOverride(Size availableSize)
{
foreach (UIElement child in Children)
{
child.Measure(availableSize);
// Do stuff with the Desired Size. (This is an example)
resultSize.Width += child.DesiredSize.Width;
resultSize.Height = Math.Max(resultSize.Height, child.DesiredSize.Height);
}
//.. Other Measure Stuff
}我放在面板上的所有标准控件都能正常工作。但是我的自定义控件将DesiredSize设置为非常小的宽度(5个像素)。尽管它上面有一个Label,但它至少需要40个像素才能显示。
所以我把这些代码放到我的自定义控件中:
protected override Size MeasureOverride(Size constraint)
{
var baseSize = base.MeasureOverride(constraint);
var labelTextWidth = GetStringWidth(label.Content.ToString(), label);
if (baseSize.Width == 0)
baseSize.Width = 100;
if (baseSize.Width < labelTextWidth)
baseSize.Width = labelTextWidth;
return baseSize;
}这将返回一个正确的Size。但是在我的面板的MeasureOverride中,child.DesiredSize并不反映我从子控件的MeasureOverride返回的内容。
,知道它为什么会这么做吗?我能做些什么才能让它正确地将计算出来的度量传递给DesiredSize?中的面板(您不能仅仅设置DesiredSize)。
发布于 2015-01-12 16:20:39
如果自定义控件的MeasureOverride返回正确的大小,但它没有反映在DesiredSize属性中,我只有一个建议。您的子元素由某个容器包装,您可以在自定义面板的MeasureOverride过程中检查子元素的类型。
https://stackoverflow.com/questions/15438271
复制相似问题