0

I have the following

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;
  }
}

Now I put that button just in an empty window like that:

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

  }
}

For some reason that button is not visible in the window. Does anyone know why? If I remove the overrides the button is displayed correct ofcourse...

edit: the reason is that the button is integrated in a complex layout where there are a lot of them and the layout pass is consuming a lot of time but the button's size will be always the same

DerApe
  • 3,097
  • 2
  • 35
  • 55
  • 1
    Where's the sense of all these overrides?. Just set Width and Height. Anyway, you may need to call the base class methods. – Clemens Apr 23 '18 at 12:37
  • Like Clemens suggested I don't see the point but I recommend calling the base functions after you've done your work. It is likely the base is handling some of the key values used to update the object properly. – Michael Puckett II Apr 23 '18 at 12:40

1 Answers1

0

Even if you want to contrain the size, you still should call the base methods for the child elements of the Button to be measured and arranged as expected:

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;
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • yea, i tried that with another control and it worked there. I guess the reason is that the button has children which needs to be arranged as well – DerApe Apr 23 '18 at 12:45
  • Yes, that's what I thought I wrote in the answer. – mm8 Apr 23 '18 at 12:45