Custom fonts in a CustomTheme library - wpf

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

Related

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>

Xaml Grid Styling

New to WPF, I'm having some trouble creating a styles in my code, I was able to make some buttons styles by drawing rectangles and making them into buttons, this opened a template editor so I was able to do it.
Now I'm wanting to create a template for a repeating stackpanel/grid layout, and I wrote it by hand this time, but I am getting an error that says the "template is not a valid member"
This is the kind of thing I was trying to create, but the Property="Template" bit is underlined in red. Can somebody explain to me the reason behind this? How do I create or initialize the template?
<Style x:Key="LaneStyle" TargetType="{x:Type Grid}">
<Setter Property="Width" Value="760"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Grid}">
<!-- Things here -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
If someone could direct me to a tutorial on styles/templates that would be nice as well, haven't been able to find one that explained it in more detail.
Grid is not a control, therefore you cannot apply a ControlTemplate to it. If you're looking for a "repeater" kind of thing, you should be using an ItemsControl.
The best way to create templates/styles is by using Microsoft Blend 3.0/4.0
Over there one can easily find out what's the progress after doing each change.
In your case, a grid cannot be styled as it is a container not a control. If you wish to customize some control need to modify the control template of the control.

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.

WPF style basedon current

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.

Resources