Click here to Skip to main content
15,394,645 members
Articles / Desktop Programming / WPF
Technical Blog
Posted 24 Dec 2010

Tagged as

Stats

22.1K views

If Your Custom WPF Control Just Would Not Show Up

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
27 Dec 2010Apache
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.

XML
<!-- 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
This article was originally posted at http://www.ikriv.com/blog?p=768

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0

Share

About the Author

Ivan Krivyakov
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.

Please visit my web site: www.ikriv.com.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 1532234012-Aug-21 13:21
MemberMember 1532234012-Aug-21 13:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.