How to display external fonts in WPF xaml designer? - wpf

I am able to display external fonts during run time, but not in the designer.
Most of the topics I came across are for the opposite scenario where it displays in designer, and not during run time.
How do I display external fonts in both design and run time?
Below are my current code.
Resource Dictionary (Dictionary.xaml)
<FontFamily x:Key="Montserrat">pack://application:,,,/xxx;Component/Fonts/#Montserrat</FontFamily>
<Style x:Key="TBRoman" TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="{StaticResource Montserrat}" />
</Style>
User Controls
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/xxx;Component/Dictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Controls
<TextBlock Text="message" Style="{StaticResource TBRoman}" />

Related

How to set windows 10 title bar button style on all app windows with MahApps

I'm new to WPF, and I am using MahApps. I want to set Windows 10 style titlebar buttons on all windows in my app. I can do this separately for each window by adding
<ma:MetroWindow.WindowButtonCommands>
<ma:WindowButtonCommands Style="{DynamicResource MahApps.Styles.WindowButtonCommands.Win10}" />
</ma:MetroWindow.WindowButtonCommands>
to each window xaml. However, I would like to set this as a style in App.xaml so I don't have to duplicate this in every window. I tried
<Application.Resources>
<ResourceDictionary>
<Style x:Key="WindowStyle" TargetType="{x:Type ma:MetroWindow}">
<Setter Property="WindowButtonCommands">
<Setter.Value>
<ma:WindowButtonCommands Style="{DynamicResource MahApps.Styles.WindowButtonCommands.Win10}" />
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Application.Resources>
but it only works for the first window. Subsequent windows opened crash with the error "Specified element is already the logical child of another element. Disconnect it first." Apparently, only one instance of WindowButtonCommands is instantiated and can only be assigned to one window. I'm not sure where to go from here.
Try to define the WindowButtonCommands element as a separate non-shared resource:
<Application.Resources>
<ResourceDictionary>
<ma:WindowButtonCommands x:Key="commands" x:Shared="False" Style="{DynamicResource MahApps.Styles.WindowButtonCommands.Win10}" />
<Style x:Key="WindowStyle" TargetType="{x:Type ma:MetroWindow}">
<Setter Property="WindowButtonCommands" Value="{StaticResource commands}" />
</Style>
</ResourceDictionary>
</Application.Resources>

MahApps Metro setting Window Border styles from App Resources

I am trying to set Window Border styles for my MahApps Metro app. I have read the articles about how to set the different Border styles and I think I get it. However, I am trying to set the Border Style for all windows in my app to be the same (all Drop Shadow) and it doesn't seem to want to work.
I have app resources that look like this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/GSDXThemes;component/GSDXDarkYellowTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
My Resource dictionary looks like this:
<!-- Merge in ResourceDictionaries defining base styles to use. This theme is based on the Metro Dark Yellow theme. -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Yellow.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />
<ResourceDictionary Source="pack://application:,,,/GSDXThemes;component/GSDXControlStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
The GSDXControlStyles dictionary just sets some custom style values for my app. It is in this file that I try to set the Window Borders.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:resx="clr-namespace:GSDXThemes.Properties"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:GSDUserControls="clr-namespace:GSD.CommonGUI.UserControls;assembly=CommonGUI">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Now customize the theme for our use...mostly just changing font sizes, etc...-->
<Style TargetType="{x:Type Controls:MetroWindow}" >
<Setter Property="WindowTransitionsEnabled" Value="False" />
<Setter Property="EnableDWMDropShadow" Value="True" />
</Style>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource MetroLabel}">
<Setter Property="FontSize" Value="16"/>
</Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource MetroTextBox}">
<Setter Property="FontSize" Value="16"/>
</Style>
...
All the other style settings work fine. But the first line for setting the Window Border does nothing. All my windows show with no border.
How can I get this to work so all Windows have the Drop Shadow border?
you must give your style a key to get a working solution
<Style x:Key="CustomDefaultWindowStyle"
TargetType="{x:Type Controls:MetroWindow}"
BasedOn="{StaticResource {x:Type Controls:MetroWindow}}" >
<Setter Property="WindowTransitionsEnabled" Value="False" />
<Setter Property="EnableDWMDropShadow" Value="True" />
</Style>
now use this style on all your MetroWindows
<Controls:MetroWindow x:Class="YourWindowClass"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:System="clr-namespace:System;assembly=mscorlib"
Title="Custom Window Style Demo"
Height="600"
Width="800"
WindowStartupLocation="CenterScreen"
Style="{DynamicResource CustomDefaultWindowStyle}">
...
</Controls:MetroWindow>
(don't be afraid of the'Invalid style target type:...' message, it's a VS bug)
hope that helps

Mixing general WPF styles with ResourceDictionary

I came from web development and WinForms to WPF and maybe I didn't get the concept yet.
I'm able to define general styles for my Application in the app.xaml. For example I defined the style for all my ribbon controls in this file.
Then I tried Microsoft Blend and came across ResourceDictionary, which is somekind of Resource File .resx I knew from WinForms.
But as I see it's not possible to mix these two concepts. For example following xaml code will not work because ResourceDictionary have to be the only child.
<Application x:Class="Wpf.MyApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
StartupUri="MyMainWindow.xaml">
<Application.Resources>
<!-- Resources scoped at the Application level should be defined here. -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/RibbonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<BitmapImage x:Key="IconDokumentNeu" >Images/NewDocument_32x32.png</BitmapImage>
<SolidColorBrush x:Key="LightGrayBrushKey">WhiteSmoke</SolidColorBrush>
</ResourceDictionary>
<Style TargetType="{x:Type ribbon:RibbonWindow}">
<Setter Property="Icon" Value="../time2_32.png" />
<Setter Property="TextOptions.TextFormattingMode" Value="Display" />
</Style>
</Application.Resources>
</Application>
It seems I didn't really get the concept. Maybe you can help me, why this is not possible and how I can use general styles next to ResourceDictionary.
You already have resources defined "next to" the dictionary, one image and one brush.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Dictionaries from file here -->
</ResourceDictionary.MergedDictionaries>
<!-- Other resources here -->
</ResourceDictionary>
</Application.Resources>
Just include the {x:type} style in the resource dictionary
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Dictionaries from file here -->
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type ribbon:RibbonWindow}">
<Setter Property="Icon" Value="../time2_32.png" />
<Setter Property="TextOptions.TextFormattingMode" Value="Display" />
</Style>
</ResourceDictionary>

WPF style problem

I am actually using windows classic style in my applications by using the following declaration
<ResourceDictionary Source="/PresentationFramework.Classic;V3.0.0.0;31bf3856ad364e35;component/themes/classic.xaml" />
But whenever i declare a style to any of my controls say to set the font size and font family the appearance of the control also changes to suit the system theme and thus the control loses the classic appearance.
What could be happening?
I tried using
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}" >
to see if this helps me in getting back the classic theme. But it doesn't seem to work.
Try if this works
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Classic;V3.0.0.0;31bf3856ad364e35;component/themes/classic.xaml" />
<ResourceDictionary>
<Style x:Key="ExtendedButtonStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="FontSize" Value="10"/>
<Setter Property="Foreground" Value="Red"/>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Button Content="click" Height="30" Width="100" Style="{StaticResource ExtendedButtonStyle}"/>
</Grid>

XAML styles in Silverlight not being recognized

I am attempting to create a Syles.xaml file for my test Silverlight app. Here is what I have in the App.xaml file:
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:uriMapper="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
x:Class="MVCSilverlight.App"
>
<Application.Resources>
<Style x:Key="NavigationContainerStyle" TargetType="StackPanel">
<Setter Property="Background" Value="Black" />
<Setter Property="Orientation" Value="Horizontal" />
<Setter Property="Height" Value="50" />
<Setter Property="Width" Value="500" />
</Style>
</Application.Resources>
</Application>
The problem is that when I include this in the app, VS2010 does not recognize it AND when I run the application, it does not display because there are errors with attempting to find that resource name/value. Here is an example of how it is being used:
<StackPanel Style="{StaticResource NavigationContainerStyle}">
</StackPanel>
I also attempting to put the styles in a file and include it in the app.xaml but that didnt work either.
Can someone give me some ideas as to why this is happening?
That XAML looks like it should work fine as long as App is still set as the startup object in the project settings and InitializeComponent() is still being called in App.xaml.cs.
If you are putting styles in a Styles.xaml file you will need use a merged resource dictionary to merge it into either the App resources or directly into the resources of the UserControl where you are going to use it.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

Resources