#1,179 – Alignment and Margin Properties for a Custom FrameworkElement

When you create a custom control that derives from FrameworkElement, your control has access to all standard FrameworkElement behavior.  This includes the use of the Margin property, as well as the HorizontalAlignment and VerticalAlignment properties.

Below, we use margin and alignment in positioning a custom framework element within a Grid.

    <Grid ShowGridLines="True">
        <loc:MyFrameworkElement HorizontalAlignment="Center"
                                Height="40" Width="100"/>
        <loc:MyFrameworkElement Grid.Row="1" Margin="20,10"/>

(Showing grid lines for clarity).