Bubbles
Bubbles

Reputation: 261

Rectangle Stretch Property Behavior

Can someone explain my misunderstanding of the Rectangle's Stretch property when setting it to NONE? According to MSDN, the definition for this value is "The content preserves its original size." Look at the code below. When I set Stretch = NONE on the fourth rectangle, it disappears.

<Grid Margin="20">
    <Rectangle Height="Auto" Width="2" HorizontalAlignment="Left" VerticalAlignment="Stretch" Fill="Black"/>
    <Rectangle Height="2" Width="10" HorizontalAlignment="Left" VerticalAlignment="Top" Fill="Black"/>
    <Rectangle Height="2" Width="10"  HorizontalAlignment="Left" VerticalAlignment="Bottom" Fill="Black"/>

    <Rectangle Stretch="None" Name="Right" Height="Auto" Width="2" HorizontalAlignment="Right" VerticalAlignment="Stretch" Fill="Black"/>
    <Rectangle Height="2" Width="10" HorizontalAlignment="Right" VerticalAlignment="Top" Fill="Black"/>
    <Rectangle Height="2" Width="10" HorizontalAlignment="Right" VerticalAlignment="Bottom" Fill="Black"/>
</Grid>

Why is this happening? This code is an excerpt from a paging control I'm using on a custom chart. I am wrapping the paging control in a ViewBox to allow auto-resizing, but I do not want my border markers resizing (the example above is what the page border markers look like).

Upvotes: 2

Views: 1616

Answers (1)

Marat Khasanov
Marat Khasanov

Reputation: 3848

Rectangle class uses private _rect field for rendering.

Here is the code of the Rectangle.OnRender:

protected override void OnRender(DrawingContext drawingContext)
{
    ...
    drawingContext.DrawRoundedRectangle(base.Fill, pen, this._rect, this.RadiusX, this.RadiusY);
}

Now let's look at the ArrangeOverride method:

protected override Size ArrangeOverride(Size finalSize)
{
    ...    
    switch (base.Stretch)
    {
        case Stretch.None:
        {
            this._rect.Width = (this._rect.Height = 0.0);
            break;
        }    
        ...
    }    
    ...    
    return finalSize;
}

It seems that when Stretch in None there is only empty rectangle. You may see it only adding a stroke. Maybe it's a bug.

Upvotes: 1

Related Questions