I'm trying to create a control in Silverlight that inherits from Button so that I can perform a specific action everytime it is clicked. I'm doing this because I'd like to reuse this custom button in several locations with the same functionality.
I'd like to create the control in such a way so that I have a can set the custom Button's Content to a specific default icon image, but still have the rest of the button's style coming from either the default button style, or being automatically set by the toolkit Themes.
I'd also like to have the Content be described and editable in XAML rather than code if possible.
It seems like this would be a pretty common problem for Silverlight developers - is there a good way to tackle it?
If you use a normal button and edit an "Empty Template", then you can style the button to have any content you wish and expose properties that you can set in the XAML for Icons etc.
By using the standard button control, you will have all the behaviors that you require.
I believe this is what you're looking for, if not can you expand on your question.
--EDIT--
Ok, I get what you are trying to do now. So what you might want to consider is creating a custom button class that inherits from Button. Then you can override the OnClick method to handle your logic. When it comes to the XAML, you can create a template style for a TargetType of your custom button class, that would be styled to your requirements.
HTH, if you need some examples place a comment and I'll mock up some examples
Related
I have created an app which uses the storyboard feature in Xcode.
I need to put a button on a few views which is a "Call us" button. The button will have the same look, text and action associated with it.
It's that reason which I thought would be best to create a subclass of the button and set all the button properties from there.
There are no coding problems, but when I drag on a button to a view on the storyboard, change it's class to the "CallButton" subclass name - it simply ignores all the formatting.
Am I missing something? Is there an easier way to reuse a button? Normally (old school way) I would simply call the button from the code, but as I've used the storyboard, I need to drag and drop the button really.
Thanks for any advice!
Xcode wont reflect the changes you make programatically in your subclass, but if you're setting up the buttons look in your subclass it should look as expected when you run the application.
Ideally you would setup your CustomButton in its initWithFrame and initWithCoder methods.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html
I want to create button that always has the same behaviour (close window). Can I add this behaviour to a template?
Templates in WPF are used for layout and data. I am assuming you are looking to attach your code-behind actions to the button template. This won't be done through a template. Instead, you probably want to look at building a custom control. This way you can put your code-behind and your button together.
Here is an article on WPF templates and how they work:
http://wpf.2000things.com/tag/templates/
Here is a forum post on rolling your own button custom control:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a81ed36b-c9d1-4619-96e2-4025b919819c/
Finally, here is a good article from MSDN on all of these issues:
http://msdn.microsoft.com/en-us/magazine/cc163421.aspx
I am creating a custom control derived from the one of the Standard WPF controls. The control has several constituent parts,and I am only modifying one of those parts.
Here's my question: If I am modifying only one part of a control, do I have to declare the control as lookless and reproduce the entire control template for the modified control in Generic.xaml, or can I omit the lookless declaration (found in the static constructor provided by Visual Studio) and simply modify the control template for the part I am extending?
I have tried the latter approach, and my control template is being ignored. I would like to get confirmation before I reproduce the entire control template, since what I am extending is the WPF Calendar. Thanks for your help.
It sounds like your best bet is to paste the entire template and modify the parts you need, although you didn't say exactly what you want to do or post any code.
Obviously if what you want to change about the calendar has a property you can modify in xaml, then that is easier. The opposite extreme would be to create a custom control (subclass).
I wanted to change the color or the ComboBox arrow the other day and the easiest way to do that was to past the entire template into a style and apply as needed, after changing one single part of the template (the arrow color, of course). There is no exposed DP to change for this and I didn't need anything more complicated than that.
HTH,
Berryl
The declaration that's generated for you by default is simply allowing for a default implicit Style to be defined for your control instead of just taking on the default Style of the base type.
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
What makes a control lookless isn't any specific declaration but rather it's definition in a code file which will then have some ControlTemplate applied to it at runtime. The alternative is the UserControl style of declaring a XAML+code-behind class which compiles into a single class with both UI and logic.
A simple example: Button is not the thing you see on the screen and click on; Button is a control that can take a single piece of Content and translate a user click into a Click event or Command call. What you see on the screen is just a visual template on top of Button's inherent behavior and state.
I have a datagrid, and certain columns need to contain text that is linked to a detail window. So, in order to make it a bit easier on myself, I created a UserControl that is basically a Button with a control template that contains a TextBlock (I could have done this a number of other ways, I know, but I figured the button already exposes a Click event, so why not?). Things are getting a bit hairy, though, when it comes to styling: I'd like to give the text a "hyperlink" sort of format--blue text, underlined--so that it's clear they are links (also, so that they resemble to format in the legacy WinForms application I'm re-implementing). But I would also like to be able to style the text--ideally, it should grab things like text color if text color is set in a style on the parent cell.
Basically, is there an easy way to implement a custom UserControl that will a) grab styles from a parent element and b) apply its default styles in a low-priority way, i.e. only apply a specific style if there's not already one set from the parent? I know I can pass the parent's style manually through a binding, but I was wondering if there was an easier way.
It sounds like you need to create a true Control or ContentControl implementation for this, so that you can override the true styles and templates.
UserControls are not really stylable, unless you start somehow creating custom properties for binding Styles.. but none of that will be implicit.
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 <Button.Content> 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 <Button ...> 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...
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.