简体   繁体   中英

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.

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.

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