PETZOLD BOOK BLOG
| Recent Entries | ||
| < Previous | Browse the Archives | Next > |
| Subscribe to the RSS Feed |
October 19, 2007
New York, N.Y.
Got an email this morning from someone trying to make the WPF CheckBox control look a little different. When you set an image or drawing as the content of a CheckBox, the checkmark box is vertically centered, like in the image on the left:
He wanted the checkmark box positioned on the upper left, as in the second image.
The default template for the CheckBox is basically a BulletDecorator; its Bullet property is set to a BulletChrome object (that's the checkmark box) and its Child property is set to a ContentPresenter (that's for the content of the control). After fooling around setting a few alignment properties, I couldn't get that checkmark box to budge. I suspect the vertical positioning is hard-coded in the ArrangeOverride method in BulletDecorator.
So I re-thought the template's visual tree: Rather than a BulletDecorator I began with a Border. I set the Child to a Grid with two auto-width columns. The first column contains a BulletChrome object with its VerticalAlignment property set to Top. The second column is the ContentPresenter. Everything else in the template is identical to the default CheckBox template (which I snagged using the DumpControlTemplate program from Chapter 25 of my book Applications = Code + Markup). This new template doesn't use a BulletDecorator element at all.
Here's the XAML file that produced the screen shot shown above:
UpperLeftCheckBoxTemplate.xaml
The new template is in the Resources section. I cleaned up some of the syntax of the default template and just commented out the old visual tree.
| Recent Entries | ||
| < Previous | Browse the Archives | Next > |
| Subscribe to the RSS Feed |
Comments:
I am "someone". I am flattered to see a blog entry about my question. Thank you for your reply.
Actually I did try to 'Grid' it, but I forgot why I gave up keeping going this way.
I have one more question: why the outer Border is needed?
— Vincent Cheng, Fri, 19 Oct 2007 13:15:15 -0400 (EDT)
The original template set the Background of the BulletDecorator to white, presumably because the bullet needs to show up against the background, so I figured I should add something to replace it. I see now I could have left out the Border and just set the Background of the Grid. — Charles
Is there's any way to email subscribe to your wpf blog ?
— Guy, Sat, 3 Nov 2007 19:42:49 -0500 (EST)
No, but I have an RSS feed. — Charles
Submit comment:
NOTE: Comments are examined personally and generally posted within 12 hours.