Search code examples
silverlightvisual-tree

How do I add an element to the visual tree in Silverlight


Here is my specific problem:

Xaml:

<local:ShrinkableContentControl x:Name="m_ShrinkableContentControl">
    <Border Background="SkyBlue">
        <Button Click="Button_Click_1" Content="Hello"/>
    </Border>
</local:ShrinkableContentControl>

Code for ShrinkableContentControl:

[ContentProperty("Shrinkable")]
public class ShrinkableContentControl : FrameworkElement
{
    protected override Size MeasureOverride(Size availableSize)
    {
        return base.MeasureOverride(availableSize);
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        this.Shrinkable.Arrange(new Rect(0, 0, 100, 100));
        return base.ArrangeOverride(finalSize);
    }

    public FrameworkElement Shrinkable { get; set; }
}

The issue here is, Shrinkable is Content, and is not added in to the visualtree, so nothing gets displayed.

Can anyone tell me if there is a way to add the Shrinkable as a child of ShrinkableContentControl in the visualtree?

Thanks, Henry


Solution

  • If you want a single child, it sounds like you should inherit from ContentControl:

    Represents a control with a single piece of content of any type.

    Just set the Content property