There are lots of articles online about how to embed custom fonts, create font styles and apply them to controls. But how can one override the global application font such that each control uses that font instead of setting the FontFamily property manually for each control. In my case, I do not wish to use a custom font as the global font but a system font such as Tahoma or Calibri.
In winrt default Fontfamily for all controls ContentPresenter is Segoe Ui and its key is ContentControlThemeFontFamily.
-*You can change or override fontfamily of button,comboboxitem,listboxitem etc from resourcedictionary because they have template property(contenpresenter or itempresenter)
1) Go to StandardStyles.xaml
2) Add <FontFamily x:Key="ContentControlThemeFontFamily">Tahoma</FontFamily>in default and Highcontrast resourcedictionary of ThemeDictionaries like below.
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<x:String x:Key="BackButtonGlyph"></x:String>
<x:String x:Key="BackButtonSnappedGlyph"></x:String>
<FontFamily x:Key="ContentControlThemeFontFamily">Tahoma</FontFamily>
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrast">
<FontFamily x:Key="ContentControlThemeFontFamily">Tahoma</FontFamily>
<x:String x:Key="BackButtonGlyph"></x:String>
<x:String x:Key="BackButtonSnappedGlyph"></x:String>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
But Textblock is not having template property so you can change its property like below
1. First Method
<TextBlock FontFamily="{StaticResource ContentControlThemeFontFamily }" >dfdsfsdf</TextBlock>
2. second method
<Page
x:Class="App3.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App3"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" FontFamily="{StaticResource ContentControlThemeFontFamily}">
<Grid>
<TextBlock>Hello World</TextBlock>
</Grid>
Related
I'm working on a WPF application using C#. So far, I have added a custom font using a ResourceDictionary on my application. When I run the app I can see the font works fine, but on the Design tab of my XAML it always shows the default font. Could I be missing something to set my designer correctly?
App.xaml
<Application x:Class="Bali.Chat.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Bali.Chat"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/Fonts.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Fonts.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<FontFamily x:Key="LatoRegular">pack://application;,,,/Fonts/#Lato Regular</FontFamily>
<FontFamily x:Key="LatoThin">pack://application;,,,/Fonts/#Lato Thin</FontFamily>
</ResourceDictionary>
MainWindow.xaml
<Window x:Class="Bali.Chat.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Bali.Chat"
mc:Ignorable="d"
Icon="Images/Logo/logo-small.png"
WindowStyle="None"
AllowsTransparency="True"
Title="Bali Chat"
Height="250" Width="400">
<StackPanel>
<TextBlock Text="Default Font!" FontSize="40" />
<TextBlock Text="Thin Font" FontSize="40" FontFamily="{StaticResource LatoThin}" />
<TextBlock Text="Regular Font" FontSize="40" FontFamily="{StaticResource LatoRegular}" />
</StackPanel>
</Window>
This is what I get on my designer
And this is what I get if I run the application
I really would like to preview what I'm trying to render without having to start the application or check it on the live preview. The tutorial I am following does show the presenter seeing the proper font in the designer, so I know there are some issues. Thanks in advance.
I want to force the Aero style on my forms. I created a "WPF Class Library" and added a form and controls to this form. As the library will be called by 3rd party C# applications running on different OS, I want to force to always apply the Aero style.
But as it is a WPF class library I have no App.xaml file where I could put my Resource Dictionary.
I placed it in a dedicated custom Styles.xaml therefore and in my form I reference it like
<Window.Resources>
<ResourceDictionary Source="Styles.xaml">
</ResourceDictionary>
</Window.Resources>
where Styles.xaml looks like
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyLib">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Aero;V4.0.0.0;component/themes/Aero.NormalColor.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
and is set to "Build action: Embedded resource" and is stored in the main project folder. I also added a reference to PresentationFramework.Aero in my project.
When I call my library form from the C# application, I get an error saying
Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' Line number 'x' and line position 'y'
What did I do wrong?
This should work for you.
<ResourceDictionary Source="pack://application:,,,/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml" />
Refere this MSDN article for more details.
UPDATE
Include ResourceDictionary in PCL
Create a ResourceDictionary
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<!-- store here your styles -->
</ResourceDictionary>
You can use it from Your PCL in WPF App
<Window x:Class="Test.Window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window"
Height="300"
Width="300">
<Window.Resources>
<ResourceDictionary Source="pack://application:,,,/Your.Base.AssemblyName;component/YourResDictionaryFolder/Dictionary1.xaml" />
</Window.Resources>
<Grid>
</Grid>
</Window>
Your.Base.AssemblyName = Dll name
YourResDictionaryFolder = Folder where you created your ResourceDictionary
Dictionary1.xaml = File name which you created above
I have a DataTemplate I'm trying to define and inside of that I have a Button I would like to set the style of. Currently I have a UserControl with MergedDictionaries... probably better to show some code:
<UserControl.Resources>
<ResourceDictionary.MergedDictionaries>
<!-- MyButtonStyle is in this dictionary -->
<ResourceDictionary Source="ms appx:///Dictionaries/ButtonStyles.xaml"/>
<ResourceDictionary>
<DataTemplate x:Key="MyDataTemplate">
<Grid>
<!-- Here is the button I want to apply the style to -->
<Button Style="{StaticResource MyButtonStyle}"/>
</Grid>
</DataTemplate>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</UserControl.Resources>
I want to set the style of Button inside the DataTemplate to MyButtonStyle which is located in my ButtonStyles.xaml. But I'm getting the following error:
Resource `MyButtonStyle` is not found
How do I correctly reference the resource in my example?
StaticResource will search resources in following order:
All resources that are declared earlier in the same dictionary
Resources in the MergedDictionaries
Application resources
It will not search in the sibling merged dictionaries.
In your case, MyButtonStyle is not included in the ResourceDictionary with MyDataTemplate, nor in any of its MergedDictionaries. What you need is:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- MyButtonStyle is in this dictionary -->
<ResourceDictionary Source="ms appx:///Dictionaries/ButtonStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="MyDataTemplate">
<Grid>
<!-- Here is the button I want to apply the style to -->
<Button Style="{StaticResource MyButtonStyle}"/>
</Grid>
</DataTemplate>
<ResourceDictionary>
</UserControl.Resources>
#nevermind's commend about the order was very precise. However, in order to import a ResourceDictionary is used the following path
<ResourceDictionary Source="/MyAssemblyName;component/Folder1/Folder2/Res.xaml"></ResourceDictionary>
Quite odd scenario. I have the following App.xaml
<Application x:Class="BrokenBG.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="WindowStyleBase" TargetType="ContentControl" >
<Setter Property="Background" Value="Red" />
</Style>
<Style x:Key="WindowStyle" TargetType="{x:Type Window}" BasedOn="{StaticResource WindowStyleBase}" />
</Application.Resources>
</Application>
And a plain empty window with the style set:
<Window x:Class="BrokenBG.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Style="{StaticResource WindowStyle}">
<Grid>
</Grid>
</Window>
When I run the app all is fine. I see the red bg.
But I cannot see it in the designer. The code above is a repro case I've experienced in another larger project.
When I change Window Style from WindowStyle to WindowStyleBase then I can directly see the red background color in the designer.
Can this be fixed? My windows have a dark theme, therefore I cannot design the views in Visual Studio / Blend since the background is white and black during runtime (my text is white)
It looks like the designer has issues with BasedOn parsing? (unsure)
One thing that works, even though it won't compile.
You can change staticresource in the based on to dynamic resource.
It'll suddenly start working in designer, but this won't compile.
I even changed red to blue, and the designer updated.
I use the Expression Dark Theme in my WPF application.
Also I have to use Extended WPF Toolkit Controls.
How I can apply this theme to them?
XAML:
<Application x:Uid="Application_1" x:Class="Mega.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
SessionEnding="App_SessionEnding" >
<Application.Resources>
<ResourceDictionary x:Uid="ResourceDictionary_1" >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Uid="ResourceDictionary_3" Source="/Themes/ExpressionDark.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
You should create your own styles for each custom control from extended toolkit, because standard themes support controls from .NET Framework like button textBox etc, look into the xaml code of theme and create your style in the same way.