I "re-themed" Button by putting a style with TargetType (but no x:key) in a ResourceDictionary and telling other developers to merge it. Now, if they want to further style the control, they need to use BasedOn.
Is there a way I could have themed Button without requiring consumers to used BasedOn?
Related
I have a Silverlight application with a custom control MyDataGrid which is a DataGrid with some extra features.
This is part of the project in which it is used. It also requires a custom style. I can add the style to my style resource dictionary and set TargetType="sdk:DataGrid" which works.
However, my style relies on a property MyProperty specific to MyDataGrid, so a warning appears in the editor. More importantly, the styles cannot be previewed in the designer.
They DO work when the code is run, however.
My question is: Is there a way to pull in local controls to a resource dictionary, rather than creating a separate project, compiling it to a .dll, and pulling in that assembly?
Thanks!
It sounds like you made a custom control, but you haven't defined a default style for it. Try the following:
In your project, add a folder at the root level called Themes.
In the Themes folder, add a ResourceDictionary named generic.xaml.
Add all of your MyDataGrid styles to generic.xaml.
Make sure one of the styles is an implicit style (using BasedOn is a great solution here).
In the default constructor for MyDataGrid, add the line DefaultStyleKey = typeof(MyDataGrid);
If you want to access part of the style (which is defined in XAML) from code, you should name that XAML item as "PART_something". The design tools understand the PART_* syntax plus that's an indication to library consumers that if you retemplate the control, you must define the required pieces prefixed with PART_.
Here are the basics on how to define a custom control.
I have created a user control in silver-light.
I
will use this control in two different applications.
Each application has its own styling rules.
I want user control to adopt styling defined in the respective application.
One approach I found is
1) Define two separate styling template of same key in the App.xaml of both application.
2) Use static resource binding to bind styling to the control.
Is there other approach to do the same.??
If the style does not change within a single application I would define a style like this:
<Style TargetType="MyUserControl />
If it changes per situation/view, apply a resource key and set the style per instance of the control.
I just want to wrap a standard control with some more additional properties (look stay the same, I don't want to do theming in first stage).
Can I just inherit from this standard control instead of UserControl or Control ? In fact I read it is obligatoryb to use Custom Control Project Template and not UserControl ontrol Project Template. Why ?
Update: I try with a Custom Control Project and inherit from the standard slider but I have nothing show up visually ! Now what should I do to have the same visual slider as the standard one ?
I know the difference between a user control and a custom control but in practice how do you do when you just want ONE single standard control ? How will a slider for example resize AUTOMATICALLY if I encapsulate it inside a User Control instead of a Custom Control ?
A custom control is a single control and can derive from another control, this would support styling. A UserControl is a composite control out of many different controls, and as a whole, doesn't support styling (the parts do however).
If you want to add features of any kind to an existing control, derive from it. If you want to pack several controls together to make it easier to handle them (you could still add DP's to it), use a UserControl.
A custom control alone won't do anything related to resizing etc, that is dependent on the settings you supply to it from the outside (ie. HorizontalAlignment, VerticalAlignment and others) when you used it in a container. The custom control should inherit the default template from the base class unless you override it.
I have managed to style a button such that there is a the background I wanted (Normal). However, I want to create an OuterGlowBitmapEffect on the content. Also, when I modify the style, it reverts to the default for MouseOver, Pressed, and HasFocus.
Here is the image of what I want, how could you do this using WPF Styles
http://tinypic.com/r/2s9ybfs/5
Without seeing your code I can't say for sure, but you need to add the "OuterGlowBitmapEffect" to the MouseOver, Pressed and HasFocus styles as well.
All these states come as a package and need to be modified in concert.
If you can, get hold of Expression Blend. This has options for creating templates and styles from existing ones and then for modifying the individual components of each aspect of the style.
So that when I apply additional typed styles in my application I don't have to do the BasedOn thing in order for them to be merged with my custom global style. You know, like happens with Microsoft's own styles. Essentially, I want to apply my style at number 9 instead of 8.
If that's relevant: I also want to completely ignore themes and anything else that might make my application appear differently on different machines.
Edit: I want to do this for controls that I didn't make, like, Button.
It sounds like what you want to do is create a different Generic.xaml (theme) Style for a control but that isn't something that's intended for built-in controls. You might be able to do something like create an alternate theme assembly with your Styles that you can fool WPF into loading (i.e. PresentationFramework.Aero.dll) or subclassing controls you want to replace templates on. Before going down that road you should really evaluate whether it's worth the time investment. Anything you might be able to get to work is going to add complexity and be a lot of extra work just to change your local default Style declarations from
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
to
<Style TargetType="{x:Type Button}">
As far as ignoring themes, there's not much you can control as far as the automatic selection process. The standard way to do it is to copy the default Style from a specific theme into your App.xaml as your application default Style and modify as necessary but that obviously creates the situation that you're trying to get away from.
If you set the OverridesDefaultStyle property to true in your custom Style, then the theme Styles (or default Styles) will be ignored. This effeictvely makes your Style the only Style used.
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.overridesdefaultstyle.aspx