How to make Silverlight UserControl a content container?

I am creating a Silverlight UserControl where the user of the control should be able to provide custom attributes and content. I thought it would be as simple as exposing my user attributes to dependency properties and inferring from ContentControl, but apparently not.

Here are my questions regarding this:

  • I got the attribute to work as a simple dependency property, but to bind it to the user interface in the control itself I had to set the DataContext of the control in its own class (this.DataContext = this), It was dirty all over ... is there best way to do this?

  • I can get the consumer of my custom control to compile it with a child of the Content, but I don’t know how to display it in the control itself. I thought that ContentPresenter, whose content is bound to Content in the current context of the binding, but which does not work.

I would like this to work, but I'm also interested in what is recommended / best practice and why. I had a hard time dealing with these events.

+3
source share
1 answer

Note. I have no experience in silverlight, but in WPF I would do the following

, MyControl, UserControl, MyProperty. , ControlTemplate :

<UserControl.Template>
    <ControlTemplate TargetType="{x:Type SomeNamespace:MyControl}">
        <StackPanel>
            <TextBlock Text="{TemplateBinding MyProperty}"/>
            <ContentPresenter/>
        </StackPanel>
    </ControlTemplate>
</UserControl.Template>

{TemplateBinding} (OneWay!) , {Binding} DataContext. ContentPresenter ControlTemplate , Content Property.

+4

Source: https://habr.com/ru/post/1714602/


All Articles