Style Triggers Cause Control To Lose Theme - wpf

I have a WPF application using MahApps Metro for it's UI theming. I also need to use style triggers so I can appropriately determine whether a control is visible based on a property. The triggers work, but have the side effect of removing the theme. So now it looks like a default WPF unthemed CheckBox. Is there any way to preserve the MahApps Metro theme on the CheckBox?

When you assign a style, you overwrite the default style or any style applied before. If you want to extend a style, you have specify the base style using the BasedOn property.
Styles can be based on other styles through this property. When you use this property, the new style will inherit the values of the original style that are not explicitly redefined in the new style.
Specify the control type to base your style on the implicit style of the control.
<Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource {x:Type CheckBox}}">
<!-- ...your setters and triggers. -->
</Style>
Specify the key of the style that you want to base your style on.
<Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource MyCheckBoxBaseStyle}">
<!-- ...your setters and triggers. -->
</Style>
The named styles for CheckBox in MahApps can be found here on GitHub.
Please be aware that although your Visibility triggers should work, other triggers that are already defined in the control template of the CheckBox styles take precedence and you will not be able to redefine them in your own style. If you ever hit that case, you have to copy the corresponding style from GitHub into your project and adapt it to your requirements.

Related

How to impose default style on styles with Control Template in WPF

In my case, based on configuration settings grid lines should appear in different colors.
I have defined a simple style with TargetType of CellValuePresenter and I noticed that the style is applied to all columns except the ones which also have a style with target type of CellValuePresenter and have their own ControlTemplate. And I have lots of styles with ControlTemplates for CellValuePresenter across the solution, and it is not possible to change all of them to be BasedOn this new style I created.
What are my options so that the styles with ControlTemplates pickup my changes without modifying each one of them.
(Un)fortunately, styles don't apply like they do in CSS, so there's no ambiguity of what you will end up with at the end, where styles build up on each other.
If you create a style for a type, it is applied across all types in the scope of your style. This means if you include it at the top in your app.xaml, everything is affected, but if you include it only for a user control or window.xaml, it only applies to that.
If you create a style and apply it on a specific control, the local style set will override the global type based one. Unless you use BasedOn, which you specifically say you cannot do.
Unfortunately, I think you will be forced to refactor how you handle your styles to get the look you are trying to achieve, and BasedOn will be the way to go.
You can make a style BasedOn the default style for that element so that it includes all setters from the implicit style as well.
For example,
<Style TargetType="{x:Type CellValuePresenter}">
<!-- Generic Style Setters -->
</Style>
<!-- This Style will include the setters from the implicit style above -->
<Style x:Key="CustomCellStyle"
TargetType="{x:Type CellValuePresenter}"
BasedOn="{StaticResource {x:Type CellValuePresenter}}">
<Setter Property="ControlTemplate" ... />
</Style>

Is there any way to make Style apply to all items without having to declare property

I have created a custom style for DataGrid. There are several datagrids in my application, and I would like for this style to apply to all of them.
One way of doing this is to add Style="{StaticResource DataGridDefaultStyle}" to all DataGrids.
But there must be an other way of doing this, similar to adding a theme, where I would declare style, and it would automatically apply to all DataGrids.
How do I do this?
You can use the implicit style as other have stated, but put it in your App.Xaml file's Application.Resources section, this will allow the style to be applied to all DataGrids in your application.
<Application.Resources>
<Style TargetType="DataGrid">
<Setter Property="Background" Value="LightBlue" />
</Style>
</Application.Resources>
check this Using CSS Selectors for Styling in WPF
You could try using an implicit style.
To create an implicit style, you must not include a key for the style and specify the TargetType. If you do so, the style will automatically be applied to each style of the specified type.
For example, this code will be automatically applied to all DataGrid elements:
<Style TargetType="DataGrid">
<!-- style information goes here -->
</Style>

My custom control in ToolBar

I have a specific control. I want to make style for it when it is placed in ToolBar. I have found how to do it in case of Button, CheckBox and other standard controls, but how I should make it for my control?
<Style x:Key="MyStyleForCustomControl" TargetType="{x:Type NameSpace:CustomControl}">
// Your setter's for your controls go here.
</Style>
NameSpace - where your Control is present
CustomControl - Name of your control.
Is this what you want?
EDIT:
If you want Style for Toolbar in your control, simply place the style in your UserControl Resources, it will be applied to the Toolbar placed within your control. The scope for this style will be limited to your control and will be hidden outside your control.
<UserControl.Resources>
<Style TargetType="{x:Type ToolBar}">
....
</Style>
</UserControl.Resources>
Ok now you clarified your question I think I can give you a solution. It looks like you simply need to set the ItemContainerStyle of the ToolBar control for your specific user control. First declare the "my" namespace where your specific control is located, then just add something like this:
<ToolBar>
<ToolBar.ItemContainerStyle>
<Style TargetType="{x:Type my:MyUserControl}">
<Setter Property="Background" Value="Azure"/>
</Style>
</ToolBar.ItemContainerStyle>
<my:MyUserControl/>
</ToolBar>
If you wanna add other control types to the ToolBar like the Button you mentioned above, then you will need to define a custom StyleSelector instead that you will set to the ItemContainerStyleSelector property. Here is a pretty good sample of StyleSelector implementation: Style Selectors

inherit a style or find the style programmatically

I have a third party control that I assume gets a style from somewhere.
I have an subclass of that control, where I add an event handler. but now when I replace the old control in xaml with my overrided control, the style gets lost. I assume that its distinguishing between the superclass and subclass when it applies the style. How do I tell it that subclasses, like MyButton:ThirdPartyButton, should have the same style as ThirdPartyButton.
Or is there a programmatic way to see the source of the style like
ThirdPartyButton.GetDefaultStyleLocation();
Define a style in the resources which is implicitly applied:
<Style TargetType="{x:Type local:MySubclass}"
BasedOn="{StaticResource {x:Type thirdParty:Control}}"/>
This is necessary since styles are sadly not inherited.

How to apply styles to the whole silverlight application?

I have created two different grid background and radio button style in my
App.xaml.
User can select any style to change the look of the page i.e: Changing the background and style of the radiobutton.
Now When I click on the raduio button the application navigates to another page and the style disappears.
Is there a way to Set the style in application level or I need to store the styleVar as Global Var and check on the second page load and then apply the styles as per the styleVar.
Yes like Jeff Wilcox said Implicit styling is a new thing in Silverlight 4. So if you want to create a style that is the default for all the controls of that type in the range XAML file or the whole application if placed in App.xaml you would leave out the x:Key attribute.
<Style x:Key="ButtonStyle" TargetType="Button">
To use ButtonStyle you would have to write:
<Button Content="A button" Style="{StaticResource ButtonStyle}" />
Leaving out the x:Key would allow you to use ButtonStyle as default.
<Style TargetType="Button">
<Button Content="A button with style that has no x:Key value" />
Now if you'd need to create a button that doesn't have this default style, you can set the Style property of that button to be x:Null or override by setting a named style to that button.
<Button Content="Default Silverlight button" Style="{x:Null}"/>
Another new thing with Styles in Silverlight 4 is that you can create new styles that are based on existing ones. Although it wasn't your question I'll give an example:
<Style TargetType="Button" BasedOn="{StaticResource BasedStyle}">
About implicit styling in the docs: http://msdn.microsoft.com/en-us/library/system.windows.style%28VS.95%29.aspx
Implicit Styles
In Silverlight 4, you can set styles
implicitly. That is, you can apply a
certain style to all elements of a
certain type. When a resource
is declared without an x:Key value,
the x:Key value assumes the value of
the TargetType property. If you set
the style implicitly, the style is
applied only to the types that match
the TargetType exactly and not to
elements derived from the TargetType
value. For example, if you create a
style implicitly for all the
ToggleButton controls in your
application, and your application has
ToggleButton and CheckBox controls
(CheckBox derives from ToggleButton),
the style is applied only to the
ToggleButton controls.
BasedOn Styles
Starting with Silverlight 3, it is
possible to build a new style based on
an existing style. You can do this
using the BasedOn property. This
reduces the duplication of code and
makes it easier to manage resources.
Each style supports only one BasedOn
style. For more information, see the
BasedOn property.
Just leave off the x:Key part of the Style, inside App.xaml. This is a new feature for Silverlight 4.
Place the styles in question in the App.xaml file. The application objects Resources property makes Styles and other resources available across the entire application.

Resources