Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

If Your Custom WPF Control Just Would Not Show Up

0.00/5 (No votes)
27 Dec 2010 1  
If your custom WPF control just would not show up

Story in a nutshell – my custom control was not visible. Reason: we defined default style for the control in our own resource dictionary, which was merged into application resources. It worked fine until we wanted to redefine the style locally in one of the controls.

<!-- default style -->
<Style TargetType="{x:Type MyControl}">
   <Setter Property="Template">
       <Setter.Value>
           <ControlTempalte>...</ControlTemplate>
       </Setter.Value>
   </Setter>
</Style>

<MyControl>
   <!-- local style -->
   <Style TargetType={x:Type MyControl}">
       <Setter Property="Background" Value="Blue" />
   </Style>
</MyControl>

If your default style is defined in generic.xaml, WPF will merge local style with default style. If your default style is defined elsewhere, local style will completely override the default style. In this case, if local style does not define a control template, your control will end up without a template and thus will not be rendered. That is, you can snoop it, smell it, get its coordinates, make sure IsVisible is true, but it won’t show up.

Possible fixes are (in order of cleanness):

  • Move default style to generic.xaml
  • Remove local style so default style is not overridden
  • Add control template definition to the local style

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here