views:

27

answers:

1

I made myself a TransparentButton style that makes the Button portion behave the way I want it to (mouseover, enabled, etc), but what I haven't been able to do is set the content correctly in the style. Currently, I manually set everything in for every button, and clearly that stuff needs to go into the style. I have set the ContentTemplate for the style to a StackPanel that just contains an Image and a Label. The problem is, I don't know how to specify in my markup the Label's text and the Image's Source. I figured that it had to do with TemplateBinding somehow, but I've been searching like crazy and can't seem to find the information.

So, in summary, I just want a consistent button style where the button content is just a StackPanel of an Image and a Label, and I want to be able to create it in my GUI with something simple like:

<Button Style={DynamicResource TransparentButton}" Label="Click Me" Image="Images/MyImage.png" />

Any tips would be much appreciated! I hope I'm on the right track here...

+1  A: 

In order to create custom properties like this, you'll need to make a CustomControl instead of just using a Button style.

This is fairly easy, though - just make a custom control that subclasses button, and adds your new properties (as dependency properties). This will make it stylable, but also provide you the ability to enforce that those properties are always available, with the syntax you're describing (other than changing <Button to <local:MyButton).

For details, see the Control Authoring Overview on MSDN.

Reed Copsey
Thanks, Reed, as always! :) I'll look into it now and hopefully it will go very smoothly.
Dave
I guess I have another question -- let's say I didn't want to use a CustomControl. Is there something else I can do to make the Button element be able to specify the underlying Label.Content and Image.Source values? Even Blend-specific is okay in this case since this is for a GUI I'm doing exclusively in Blend 3.
Dave
Well, you can, but it's basically forcing a custom style per button, if you want these to change. It might be possible to do something with an attached property, but I'm not sure how you'd refer to that from within a style (I've never tried it).
Reed Copsey
@Reed - any idea how to get the border around a button to disappear? I am playing with the Template property and am not having much luck. I have been messing with the ControlTemplate's ContentPresenter. Is kaxaml the fastest way to test out template changes?
Dave
I've usually used blend. You can extract the template for a standard button, and replace it with your own. I believe a button has an explicit border set - you'd have to just remove that from your template. As for checking out changes - kaxaml is probably the fastest - although blend is pretty nice, since it has a lot more features.
Reed Copsey