adontz
adontz

Reputation: 1438

Custom shaped WPF control

I'm searching for a way to create a WPF Control with custom shape which will be able to contain child controls.

Simplifying the question, I need something like a non-rectangular panel.

UPDATE: I need a "real" custom shape. Not to capture mouse events outside the shape and so on.

Upvotes: 0

Views: 1563

Answers (1)

mihajlv
mihajlv

Reputation: 2315

I would derive from ContentControl, and then as child in it i would put a content presenter, in the content presenter you can put any panel such as a canvas in which you can host other elements. I would do this as a Template applied to your control and would bind the content of the presenter to the content property of the control, in such a way adding to the content property of your control will add it to the presenter which will show it. Here is an example:

 <ControlTemplate TargetType="MyCustomControl">
                <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
                    <ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
                    <Ellipse x:Name="myEllipse Background="Green"/> <!-- the control won't catch events outside it-->
                </Grid>
 </ControlTemplate>

Then just apply the template to your control and you are all set, don't forget to add a panel as the child to content if you want to host other elements in it.

Upvotes: 1

Related Questions