From your code example, it appears that you are using the wrong kind of control to extend. From the Control Authoring Overview page on MSDN:
Benefits of Deriving from Control
Consider deriving from Control instead of using the UserControl class if any of the following apply:
•You want the appearance of your control to be customizable via the ControlTemplate.
•You want your control to support different themes.
Benefits of Deriving from UserControl
Consider deriving from UserControl if all of the following apply:
•You want to build your control similarly to how you build an application.
•Your control consists only of existing components.
•You don't need to support complex customization.
Therefore, I believe that you will have much more luck if you use a UserControl
and declare your UI elements in XAML. However, if you are sure that you want to use a CustomControl
, then you should define all of your UI elements in XAML in the ControlTemplate
for your control in generic.xaml
. They will all be rendered automatically then and you won't need to refresh anything manually.
Also, you shouldn't really be using the OnRender
method to do what you could simply do in XAML. That's more often used for defining new graphics for a control that are not already available for use from other UI elements in WPF. Just create DependencyProperties
and data bind to any properties that you want to control in the UI. On thing to note is that you'll have to use a RelativeSource Binding
to access them from generic.xaml
(assuming you have added the GraphControls
XAML Namespace Prefix):
<Ellipse Fill="{Binding MarkerFill,
RelativeSource={RelativeSource AncestorType={x:Type GraphControls:Marker}}}" />