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="Tempalte">
       <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

Posted in

Leave a Reply

Your email address will not be published. Required fields are marked *