WPF style basedon current - wpf

Is there a way to create a style that extends the current style, i.e. not a specific style?
I have a WPF application where I create styles to set some properties like borders or validation.
<Style TargetType="{x:Type Button}"
BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Padding" Value="5,2,5,2"/>
</Style>
Now I want to try out some Themes to see which one works best for my application. Problem is that to do that I need to change the BasedOn property on all my Styles to the style in the Theme.
<Style TargetType="{x:Type Button}"
BasedOn="="{x:Static ns:SomeTheme.ButtonStyle}">">
<Setter Property="Padding" Value="5,2,5,2"/>
</Style>
I can do it via search/replace but it would be nice if it could be done dynamicly.

You need to do it in this way only, there is no shortcut to do this, you have to set BasedOn attribute atleast.

if you store all your resources in seperate assemblies from your ui then you can use themes to dynamically change them at runtime.
themes in seperate assembly
loading different themes at runtime

Have you tried DynamicResource instead of StaticResource.

Related

Custom fonts in a CustomTheme library

I cannot get a custom font to work in a library theme.
This is something super simple but currently driving me insane. I created a Theme library for my app's styles, colors, fonts etc...
I have a style like this:
<Style x:Key="MyTextBox" TargetType="{x:Type TextBox}">
<Setter Property="FontFamily" Value="/MyThemes;component/Fonts/#Roboto"/>
</Style>
Ignoring the custom font the style works correctly. If I change the font to a built-in Windows font it works perfectly. However when I set the custom font it doesn't. What am I doing wrong?
You can try to do using this way
<Style x:Key="MyTextBox" TargetType="{x:Type TextBox}">
<Setter Property="TextElement.FontFamily" Value="/MyThemes;component/Fonts/#Roboto"/>
</Style>
Also make sure that you properly referenced your font in theme and marked it as resource. There are also some helpful details in MSDN

Custom control not inheriting parent's styles

I'm trying to maintain a uniform look and feel across elements in my WPF application, and at the same time I want to create a modified TextBox. However, when I do this, styles that I define at the application level for TextBox aren't being applied to the class I created, even though the style created for my custom control is using the BasedOn property.
Is there something I'm missing that's causing this to behave differently than I expect?
I reproduced the issue in a brand-new WPF project in VS2010 with this setup:
C# Code:
public class CustomTextBox : TextBox
{
static CustomTextBox() {
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomTextBox), new FrameworkPropertyMetadata(typeof(CustomTextBox)));
}
}
XAML in Themes\Generic.xaml:
<Style TargetType="{x:Type local:CustomTextBox}" BasedOn="{StaticResource {x:Type TextBox}}"/>
XAML in App.xaml:
<Application.Resources>
<Style TargetType="TextBox">
<Setter Property="Background" Value="Red"/>
</Style>
</Application.Resources>
However, in the designer and when I run the app, the CustomTextBox falls back onthe default styling for the text box instead of having a red background, even though the documentation for the BasedOn property suggests that my derived class should have this styling...
There are several ways that styles in WPF can be extended or inherited. 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.
...
Note: If you create a style with a TargetType property and base it on another style that also defines a TargetType property, the target type of the derived style must be the same as or be derived from the type of the base style.
Short Answer: Your style is based on a StaticResource
<Style TargetType="{x:Type local:CustomTextBox}" BasedOn="{StaticResource {x:Type TextBox}}"/>
When you did this, you are not changing the StaticResource
<Style TargetType="TextBox">
<Setter Property="Background" Value="Red"/>
</Style>
So CustomTextBox is not supposed to inherit the red background.

WPF MahApps.Metro AnimatedSingleRowTabControl FontSize

How do I change the font size of the tabs when using the MahApps.Metro AnimatedSingleRowTabControl.
When using a normal TabControl, my theme TabItem (based on MetroTabItem) overrides the fontsize but this does not work for the animated single row tab control. I tried setting the fontsize property on the control in the XAML and this didn't work either.
Regards
Alan
You can also define the following in the Application.Resources your App.xaml:
<system:Double x:Key="TabItemFontSize">16</system:Double>
Controls.TabControl.xaml makes use of it as follows:
<Setter Property="Controls:ControlsHelper.HeaderFontSize"
Value="{DynamicResource TabItemFontSize}" />
You can do the following, setting the header font size to whatever value you want:
<metro:MetroAnimatedSingleRowTabControl>
<metro:MetroAnimatedSingleRowTabControl.ItemContainerStyle>
<Style TargetType="{x:Type metro:MetroTabItem}" BasedOn="{StaticResource {x:Type metro:MetroTabItem}}">
<Setter Property="HeaderFontSize" Value="24"/>
</Style>
</metro:MetroAnimatedSingleRowTabControl.ItemContainerStyle>
</metro:MetroAnimatedSingleRowTabControl>

What is the most economical way to implement your own window border and title bar?

I am pretty new to WPF and am sitting here with my book trying to figure out the best approach to this application.
The title bar is not part of the client area so I am making my own title bar.
Which way would it be easiest to make this into some sort of resource to apply to all new windows I create?
<Application.Resources>
<Style x:Key="WindowTheme">
<Setter Property="Window.WindowStyle" Value="None"/>
</Style>
<!--Would I create a user control here for the title bar/border and title bar buttons? Or would it be a style?-->
</Application.Resources>
In WPF, there are two ways to use styles: Named styles and typed styles. A named style has an x:Key="..." attribute. A typed style doesn't have a name, but a TargetType="..." attribute (Rem: Named styles can and very often do have a TargetType as well, so named styles and unnamed styles would be more precise). Typed styles automatically get applied to all controls in the scope, which are of type TargetType (not a derived type).
<Style TargetType="{x:Type Window}">
<Setter Property="Background" Value="Blue" />
</Style>
To create your own window, you can set it's template property to a UserControl in the style:
<Style TargetType="{x:Type Window}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The professional way to implement the control template is to implement it 'from scratch', this means not using a UserControl which derives from Window. To do this, you define the visual tree of the Window, and use the WPF feature TemplateParts to define what part of your control template is responsible for what functionality of the window.
Here is a tutorial which describes pretty exactly what you want to do:
CodeProject tutorial

How to apply WPF styles based on top of another style without using BasedOn property

I am styling CellValuePresenter (From Infragistics) to give different look to Gid Lines and have defined a style (gridLineStyle) and applied to the Grid's CellValuePresenterStyle Property.
I have discovered that there are columns for which custom templates are defined by templating CellValuePrenter and the grid lines are not visible (as expected). I can make it work by applying BasedOn property as in
<Style x:Key="gridLineStyle" TargetType="ig:CellValuePresenter">
<Setter Property="BorderThickness" Value="0,0,1,1"/>
<Setter Property="BorderBrush" Value="{Binding Path=BorderBrushForAllCells,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type pwc:CarbonBlotter}}}"/>
</Style>
<Style x:Key="anotherColumnStyle" TargetType="{x:Type ig:CellValuePresenter}" BasedOn="{StaticResource gridLineStyle}">
<Setter Property="Template">
....
<pwc:BaseXamDataGrid>
<pwc:BaseXamDataGrid.FieldSettings>
<ig:FieldSettings CellValuePresenterStyle="{StaticResource gridLineStyle}"
...
But there are many styles with custom templates, and just wondering whether I can define a style without using BasedOn property and yet inheriting default style
You can find the complete CellValuePresenter style definition in your infragistics installation folder under DefaultStyles\DataPresenter\DataPresenterGeneric_Express.xaml
You can copy that style into your App.xaml under Application.Resources, modify it as you wish and that should become your new default style for CellValuePresenter.

Resources