Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF MeasureOverride不适用于我的自定义控件_C#_.net_Wpf_Layout - Fatal编程技术网

C# WPF MeasureOverride不适用于我的自定义控件

C# WPF MeasureOverride不适用于我的自定义控件,c#,.net,wpf,layout,C#,.net,Wpf,Layout,我有一个自定义控件,它“托管”在一个自定义面板(我的和我的)中 我的自定义面板的代码如下所示: protected override Size MeasureOverride(Size availableSize) { foreach (UIElement child in Children) { child.Measure(availableSize); // Do stuff with the Desired Size. (This is an example

我有一个自定义控件,它“托管”在一个自定义面板(我的和我的)中

我的自定义面板的代码如下所示:

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像素)。即使它上面有一个
标签
,希望至少能显示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;
}
受保护的覆盖尺寸测量覆盖(尺寸约束)
{
var baseSize=基本度量超越(约束);
var labelTextWidth=GetStringWidth(label.Content.ToString(),label);
如果(baseSize.Width==0)
基准尺寸。宽度=100;
if(baseSize.Width
这将返回正确的
大小。但是在我的面板的MeasureOverride中,child.DesiredSize并不反映我从我的孩子控件的MeasureOverride返回的内容


你知道为什么会这样吗?我能做些什么来让它以DesiredSize正确地将我计算的度量值传递给面板?(您不能只设置DesiredSize。)

如果自定义控件的度量值超出返回正确的大小,但它没有反映在DesiredSize属性中,我只有一个建议。您的子元素被某个容器包装,您可以在自定义面板的度量值覆盖过程中检查子元素的类型。

请发布自定义控件的模板,以及您在面板的度量值覆盖过程中获得的可用大小。@AndrewS-我的自定义面板的模板太大,无法在此处发布(255行)。我将其作为要点加载到这里:。availableSize是{510,Infinity}