|
Architect or Cobbler?
Good code starts with good design |
|
about me
links
Blogs I follow
recent posts
archives
feeds |
Styling WPF ButtonsSaturday, November 11, 2006I was at TechEd last week and I was asked several times how to customize buttons. The answer is pretty straightforward, you use styles. If you're an ASP.Net developer that won't scare you, but if you're a Windows Forms developer then you may need a quick refresher. You can apply a style to any control, and give default values for properties, so for instance you could create a style template that always coloured your buttons red, something like this:
To use this style in your button is fairly trivial, you simply reference the key you created as in the following code:
Of course this simply allows you to change the basic properties, what if I want to change the entire look and feel of the Button control - well in that case you need to provide a control template. At first glance this appears more complex, but it isn't really that bad.
When you provide a control template you need to do all the redrawing, as it currently stands your button UI doesn't change when the button is clicked, so you need to provide a trigger to update the template as the user clicks on the button, as shown below:
You can customise any control in this way and the flexibility is incredible. Of course you could also do all of this through source code if you are completely set against XAML. You should notice the TemplateBinding directives, these simply specify that the values in the XAML will be passed through to the template, as the example below shows, the height and width attributes are passed to the underlying template:
Running this gives you the following button , and clicking it changes the GUI, so all in all a great 10 minutes work.
# posted by James @ 5:13 PM You might also want to consider template binding the ContentTemplate and ContentTemplateSelector in the button, in case developer using the button wants to tweak the template. You also don't need to bind the Width and Height of the Ellipse, as it will naturally scale to the size of the Grid (and thus the Button). # posted by @ 3:08 PM Nice article! That's an interesting point "You also don't need to bind the Width and Height". I'm a newbie and it appeared these values were picked up automatically via inheritance. However if I set the size of the Grid the Button doesn't scale to that size instead it didn't seemed to be drawn properly! # posted by martin @ 8:47 AM Post a Comment << Main blog page |
|
|
|