Skip to content

Digging into the visuals of controls built by Microsoft or others.

June 16, 2007

The visual tree defined in a ControlTemplate to define the look and feel of a control can be accessed by application developers.  I’d love to understand if there any scenarios where you find this necessary.  (Please let me know either way.)

It is a bit dangerous to do this because the contents of the VisualTree may differ with different operating system themes.

I bring this up because we have a perf problem with a control that may be best fixed by improving the ControlTemplate…but if any devs were digging into the template, that code may break.  Compat is critical, but so is perf…so gathering some info…


From → WPF

  1. Aelij permalink

    I never rely on the control template, so I vote for the perf fix.
    Actually, the one time I did rely on it was when I did this hack; but good code shouldn\’t break if the template changes.

  2. Jeff permalink

    The only time I\’ve done that is to copy the control template during development. This is so I can use my own copy of the visual tree that doesn\’t change when OS themes do, and also so that I can have a radio button that looks like a regular button.

  3. Chris permalink

    Is it worth making a sweeping change for the benefit of one control?  What does the control do?  Will it benefit other controls later on? 🙂

  4. Rob permalink

    I believe ComboBox doesn\’t use a VirtualizingStackPanel in its template by default.  This is something that people can discover, and work around, however it isn\’t a great "Pit of Success" that Rico likes us to deliver on.
    We\’re exploring options…right now I just need to know why somebody would dig into the visual tree of the items in the combobox.  If the danger/likelihood is low, we have more flexibility.
    Thanks for the input…please keep it coming.

  5. Thomas permalink

    I tried to hack the ComboBox\’s visual tree at runtime as I wanted to add a button after the dropdown button. But I never got it to work very well so I made it into two controls; a ComboBox and a Button.
    In another situation I tried to use the same Template for a custom DropDownControl-thingy and the standard CompoBox. Meaning that I just wanted to replace the ItemsPresenter with my own content, but keep all the other whistles and bells intact. But I didn’t get that to work either.. (Hmm, maybe I’m just a bad programmer 🙂 ). So my only solution was to make two similar templates.
    But in any case I agree that manipulating the Visualtree at runtime is quite “dangerous”. And if it’s a matter of noticeable performance my first impression would be to vote for performance (but I didn’t think it much through though).

  6. Thomas permalink

    BTW, I also create visualtrees by code at runtime. That would still be possible right? Cause that I certainly need still.

  7. Unknown permalink

    I try to derive from a Control and reuse the complete VisualTree of the base control. My intention is to build a Control that supports every property and theme etc of the base control plus some extra stuff. One example could be a TextBox with an optional icon in one corner. (SearchTextBox)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: