0

I'm out to write some attached properties as suggested in Pushing read-only GUI properties back into ViewModel

I've written the following unit test:

    private const double Dimension = 10.0;

    [Test]
    [RequiresSTA]
    public void Gets_ActualWidth()
    {
        var rectangle = new Rectangle() { Width = Dimension, Height = Dimension };
        double actualWidthMeasurement = Measurements.GetActualWidth(rectangle);
        Assert.That(actualWidthMeasurement, Is.EqualTo(Dimension));
    }

This is too naive though, the rectangle has an ActualWidth of 0 because no layout has been calculated.

Is there a simple way I can get a Rectangle with it's layout calculated.

I tried adding it to a StackPanel and calling Arrange(new Rect(0,0,20,20)), but still got a rectangle with ActualWidth/ActualHeight = 0.0d.


SOLUTION

    [Test]
    [RequiresSTA]
    public void Gets_ActualWidth()
    {
        var rectangle = new Rectangle() { Width = Dimension, Height = Dimension};
        rectangle.Measure(new Size(20, 20));
        rectangle.Arrange(new Rect(0, 0, 20, 20));
        double actualWidthMeasurement = Measurements.GetActualWidth(rectangle);
        Assert.That(actualWidthMeasurement, Is.EqualTo(Dimension));
    }
Community
  • 1
  • 1
Grokodile
  • 3,881
  • 5
  • 33
  • 59

1 Answers1

3

I don't see that you called Measure. That should be called before Arrange or else the Arrange will fail as everything has a DesiredSize of 0,0.

myStackPanel.Measure(new Size(20, 20));
myStackPanel.Arrange(new Rect(0, 0, 20, 20));
jHilscher
  • 1,810
  • 2
  • 25
  • 29
Ed Bayiates
  • 11,060
  • 4
  • 43
  • 62
  • That's great, thanks, I don't need the StackPanel either, see my edit for a working unit test. – Grokodile Jul 27 '11 at 18:01
  • True for this case. Some objects do need to be added to a parent, though (e.g., if you want to make sure a template gets applied). – Ed Bayiates Jul 27 '11 at 18:15