简体   繁体   中英

Silverlight - Exposing canvas within user control

Given a custom user control having a canvas within.

<Grid>
    <Border BorderBrush="Black" BorderThickness="3">
        <Canvas x:Name="CanvasDropArea" Background="#FFF7BDBD"/>
    </Border>
</Grid>

Is there a way to expose the canvas to allow other controls to be append into the above said user control?

<CustomControlWithCanvas>
   <Canvas>
        <!--other user control-->
   <Canvas>
</CustomControlWithCanvas>

I have tried exposing it as a properties but that fails, as its be treated as a normal property instead of Collection which allow UIElements to be appended. However, i was able to append controls onto the element using code behind by retrieving the canvas and adding it manually.
So what should be the correct way of exposing the canvas? to allow it be alter during design time, and retain canvas TOP and left dependency property as well.

Try this - change the type of your control from UserControl to ContentControl, put inside of your Canvas and bind the ContentPresenter's Content property to your control's Content property somehow - perhaps by naming your control and using .

In a more standard way - you would implement it as a templated/custom control instead of a UserControl, put the template in your Themes/Generic.xaml instead of MyControl.xaml, define DefaultStyleKey in your constructor, define TemplateParts etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM