0

我有以下

public class MatrixButton : Button
{
  public MatrixButton()
  {
    Height = 20;
    Width = 44;
    Content = "foo";
  }

  protected override Size MeasureOverride(Size constraint)
  {
    var measureOverride = new Size(44, 20);
    return measureOverride;
  }

  protected override Size ArrangeOverride(Size arrangeBounds)
  {
    var arrangeOverride = new Size(44, 20);
    return arrangeOverride;
  }
}

现在我把那个按钮放在一个像这样的空窗口中:

public partial class MainWindow : Window
{
  public MainWindow()
  {
    InitializeComponent();
    Content = new MatrixButton();

  }
}

由于某种原因,该按钮在窗口中不可见。有谁知道为什么?如果我删除覆盖按钮,当然会正确显示...

编辑: 原因是按钮被集成在一个复杂的布局中,其中有很多并且布局传递会消耗大量时间,但按钮的大小将始终相同

4

1 回答 1

0

即使您想限制大小,您仍然应该调用Button要测量的子元素的基本方法并按预期排列:

protected override Size MeasureOverride(Size constraint)
{
    var measureOverride = new Size(44, 20);
    base.MeasureOverride(measureOverride);
    return measureOverride;
}

protected override Size ArrangeOverride(Size arrangeBounds)
{
    var arrangeOverride = new Size(44, 20);
    base.ArrangeOverride(arrangeOverride);
    return arrangeOverride;
}
于 2018-04-23T12:44:03.063 回答