Can I set the default font using XAML?
I see the included themes use MergedDictionaries, like this:
I decided I would like to change the default font face to "Segoe UI" and the default size from 15 point to 12 point, so I did this in App.xaml:
Effectively, I replaced the Fonts.xaml resources with my own, leaving everything else the same.
This does not affect the fonts for TextBlock, Button, etc. Looking in NoesisTheme.Styles.xaml, it appears that only a few controls like groupbox, tooltips and menuitems even reference these font settings.
So, I added this code to App.OnStartup, just to prove that I could affect the fonts at all:
That seems to have worked.
The problem is that this only applies to a Noesis build. If I build the same code for WPF, it does not get these settings. The result is that the WPF target, which is the best editing environment, does not look like the eventual Noesis build.
So, can I affect the default font through XAML and have it be consistent between WPF and Noesis builds?
Code: Select all
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary ResourceDictionary.Source="NoesisTheme.Brushes.LightAqua.xaml" />
<ResourceDictionary ResourceDictionary.Source="NoesisTheme.Fonts.xaml" />
<ResourceDictionary ResourceDictionary.Source="NoesisTheme.Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Code: Select all
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Noesis.GUI.Extensions;component/Theme/NoesisTheme.Brushes.LightAqua.xaml" />
<ResourceDictionary>
<FontFamily x:Key="Font.Family.Default">Segoe UI</FontFamily>
<sys:Double x:Key="Font.Size.Header">13</sys:Double>
<sys:Double x:Key="Font.Size.Normal">12</sys:Double>
<sys:Double x:Key="Font.Size.ToolTip">10</sys:Double>
</ResourceDictionary>
<ResourceDictionary Source="pack://application:,,,/Noesis.GUI.Extensions;component/Theme/NoesisTheme.Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
This does not affect the fonts for TextBlock, Button, etc. Looking in NoesisTheme.Styles.xaml, it appears that only a few controls like groupbox, tooltips and menuitems even reference these font settings.
So, I added this code to App.OnStartup, just to prove that I could affect the fonts at all:
Code: Select all
string[] fonts = new string[] { "Segoe UI", "Arial", "Segoe UI Emoji" };
Noesis.GUI.SetFontFallbacks(fonts);
Noesis.GUI.SetFontDefaultProperties(12, FontWeight.Normal, FontStretch.Normal, FontStyle.Normal);
The problem is that this only applies to a Noesis build. If I build the same code for WPF, it does not get these settings. The result is that the WPF target, which is the best editing environment, does not look like the eventual Noesis build.
So, can I affect the default font through XAML and have it be consistent between WPF and Noesis builds?
Re: Can I set the default font using XAML?
One possible approach would be to use Application.FindResource to look at the XAML and then execute code in App.OnStartup.
See https://docs.microsoft.com/en-us/dotnet ... work-4.6.1
However, Application.FindResource is not implemented. Is that intentional or an oversight?
See https://docs.microsoft.com/en-us/dotnet ... work-4.6.1
However, Application.FindResource is not implemented. Is that intentional or an oversight?
-
sfernandez
Site Admin
- Posts: 2974
- Joined:
Re: Can I set the default font using XAML?
Hello,
In Blend if you don't specify a font in your xamls it will use the font from the operating system (Segoe UI) as the default one. In Noesis that default font is set using SetFontFallbacks and SetFontDefaultProperties. This is like the fallback value if you don't define a font family in your styles.
Instead of that approach you can explicitly define the font and its properties for all the controls and text styles in your theme:
This way the application will use those properties without specifying anything else.
In case you want to change a particular element you still can do that by setting the properties locally in the element:
But inheritance won't work as style properties will have more priority than inherited values:
I hope this helps.
In Blend if you don't specify a font in your xamls it will use the font from the operating system (Segoe UI) as the default one. In Noesis that default font is set using SetFontFallbacks and SetFontDefaultProperties. This is like the fallback value if you don't define a font family in your styles.
Usually you design the theme styles to allow property inheritance of the FontFamily and FontSize, so you can override the default font of your application or any part of the UI by just setting the properties in a root element. This is the approach we followed with NoesisTheme.So, can I affect the default font through XAML and have it be consistent between WPF and Noesis builds?
Code: Select all
<Window ...
FontFamily="{StaticResource Font.Family.Default}"
FontSize="{StaticResource Font.Size.Normal}">
</Window>
Code: Select all
<StackPanel TextElement.FontFamily="Fonts/#Arial">
<Button Content="Button 1"/> <!-- font properties inherited from the StackPanel -->
<Button Content="Button 2"/>
<Button Content="Button 3"/>
</StackPanel>
Code: Select all
<Style x:Key="BaseStyle">
<Setter Property="TextElement.FontFamily" Value="Fonts/#Arial"/>
<Setter Property="TextElement.FontSize" Value="12"/>
</Style>
<Style TargetType="TextBlock" BasedOn="{StaticResource BaseStyle}"/>
<Style TargetType="Control" BasedOn="{StaticResource BaseStyle}">
...
</Style>
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Control}}">
...
</Style>
...
In case you want to change a particular element you still can do that by setting the properties locally in the element:
Code: Select all
<Button FontFamily="Fonts/#Roboto"/>
Code: Select all
<StackPanel TextElement.FontFamily="Fonts/#Arial">
<Button Content="Button 1"/> <!-- these buttons will still use the font defined in the Button style -->
<Button Content="Button 2"/>
<Button Content="Button 3"/>
</StackPanel>
Sorry, not implemented yet, could you please report it in our bugtracker? In the meantime you can directly search for the resource in the application resources like this:One possible approach would be to use Application.FindResource to look at the XAML and then execute code in App.OnStartup.
See https://docs.microsoft.com/en-us/dotnet ... work-4.6.1
However, Application.FindResource is not implemented. Is that intentional or an oversight?
Code: Select all
Application.Current.Resources["somekey"]
Re: Can I set the default font using XAML?
Thanks for the explanation. I rolled my own FindResource and confirmed that I can set the default font characteristics during Application.OnStartup from the XAML in App.xaml. I was hoping to produce something that automatically used the same font family and size as a native WPF application, but on further thought it does not seem like a reasonable request since Noesis is cross-platform. I'll just hard-code the Windows 10 defaults into my App.xaml and that will have to do.
Re: Can I set the default font using XAML?
I am going to mark this as solved. Application.FindResource will be implemented in #2245
Who is online
Users browsing this forum: Bing [Bot], laudaicat and 7 guests