Unable to import font into WPF application - wpf

I'm trying to add a custom font to my application, but I'm getting an error:
An object of the type "System.Windows.Style" cannot be applied to a property that expects the type "System.Windows.Media.FontFamily".
From the error, I'm guessing that it means, the font wasn't imported, but I'm not sure why it wasn't.
Here's the code in the MainWindow.xaml file:
<Window.Resources>
<Style x:Key="Raleway">
<Setter Property="TextElement.FontFamily" Value="fonts/#Raleway"/>
</Style>
</Window.Resources>
<TextBlock Text="text"
FontWeight="Light"
FontFamily="{StaticResource Raleway}"
FontSize="22"
Foreground="White"
HorizontalAlignment="Center"
/>
This is my directory:

The following markup shows how to reference the application's font resources:
<Window.Resources>
<FontFamily x:Key="Raleway">./fonts/#Raleway</FontFamily>
</Window.Resources>
<TextBlock Text="text"
FontWeight="Light"
FontFamily="{StaticResource Raleway}"
FontSize="22"
Foreground="White"
HorizontalAlignment="Center"
/>
When you add fonts as resources to your application, make sure you are setting the <Resource> element, and not the <EmbeddedResource> element in your application's project file. The <EmbeddedResource> element for the build action is not supported.
It is possible to package fonts with WPF application. The detailed information is here: Packaging Fonts with Applications

Related

Only some Font Awesome glyphs display in WPF

I am attempting to use Font Awesome in a WPF app. It half works. That is, some glyphs are picked up and show, others show a rectangle.
I'm following guides correctly, I think, but something is going wrong!
Here's a code snippet
<Window.Resources>
<FontFamily x:Key="FontAwesomeRegular">Fonts/Font Awesome 5 Free-Regular-400.otf#Font Awesome 5 Free Regular</FontFamily>
<FontFamily x:Key="FontAwesomeBrands">Fonts/Font Awesome 5 Brands-Regular-400.otf#Font Awesome 5 Brands Regular</FontFamily>
<FontFamily x:Key="FontAwesomeSolid">Fonts/Font Awesome 5 Free-Regular-400.otf#Font Awesome 5 Free Solid</FontFamily>
</Window.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<TextBlock FontFamily="{StaticResource FontAwesomeSolid}" Text="" HorizontalAlignment="Left" Foreground="Red" Margin="0" TextWrapping="Wrap" VerticalAlignment="Center" FontSize="100"/>
<TextBlock FontFamily="{StaticResource FontAwesomeSolid}" Text="" HorizontalAlignment="Left" Foreground="Red" Margin="0" TextWrapping="Wrap" VerticalAlignment="Center" FontSize="100"/>
</StackPanel>
</Grid>
The fonts are installed in a Fonts directory
And Marked as Resource
And this is what it shows on the screen
You can see that the first icon xf15c; displays as expected but the second one xf15a; does not. Generally, most do not display.
Why?
If you open the .otf file, you can see the name of the font. This is what you need to use to the FontFamily resource, without the file extension.
Try the following code:
<FontFamily x:Key="Icons">pack://application:,,,/{YOUR PROJECT NAME};component/Fonts/#Font Awesome 5 Free Solid</FontFamily>

WPF IDataErrorInfo issues

I've used WPF and IDataErrorInfo in the past apps to display errors to the user via a controltemplate by putting an image in the adorner and adding a tooltip to the image like this;
<Style x:Key="textStyle" TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<Border BorderBrush="Orange"
BorderThickness="2"
CornerRadius="4"
SnapsToDevicePixels="True">
<Border.Effect>
<DropShadowEffect BlurRadius="10"
ShadowDepth="0"
Color="Orange" />
</Border.Effect>
<DockPanel>
<Image Width="16"
Height="16"
Margin="-20,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
RenderOptions.BitmapScalingMode="HighQuality"
Source="{StaticResource imgError}"
ToolTip="{Binding ElementName=adornedElement,
Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}"
ToolTipService.ShowDuration="30000" />
<AdornedElementPlaceholder Name="adornedElement" />
</DockPanel>
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
With the appropriate implementation of IDataErrorInfo in the ViewModel and setting Textbox in the view accordingly the image and tooltip are shown;
<TextBox Name="txt"
Grid.Column="0"
Height="40"
Background="Aqua"
Style="{StaticResource textStyle}"
Text="{Binding Path=Text,
UpdateSourceTrigger=PropertyChanged,
ValidatesOnDataErrors=True}" />
<TextBlock Grid.Column="1"
Height="40"
Background="AliceBlue"
Text="{Binding ElementName=txt,
Path=(Validation.Errors).CurrentItem.ErrorContent}" />
The above code displays correctly in my previous apps and shows the error in the image tooltip as confirmed by the Textblock.
However, in my current app which is built using Prism I can't get the Image to display. The TextBlock updates correctly and I can set the error to the TextBox tooltip via a style trigger without any issue. The problem is I can't seem to get the image (or anything else) to display in the Adorner. The Image is not shown and border is not changed.
The difference between previous apps and this is that the view is in a Region in a ContentControl and I've used dependency injection to inject the viewmodel into the view constructor and set the DataContext.
I can't figure out why this doesn't work when it did previously. I think I may need to include an AdornerDecorator somewhere but I'm perplexed as to where having tried it in a few places without success. Any ideas how I can ensure the Adorner is shown?
Used an AdornerDecorator to wrap the element containing the texbox and all works fine.

Silverlight 5 + Prism:TabControlRegionAdapter

I had a View using a TabControl with a prism:TabControlRegionAdapter
<sdk:TabControl Grid.Row="1" AutomationProperties.AutomationId="GUID" Margin="8,8,12,12"
prism:RegionManager.RegionName="GUID_REG_NAME"
prism:RegionManager.RegionContext="{Binding CurrentSelectedItem}" Name="TabControl1" >
<prism:TabControlRegionAdapter.ItemContainerStyle>
<Style TargetType="sdk:TabItem">
<Setter Property="HeaderTemplate">
<Setter.Value>
<!--Display the child view name on the tab header-->
<DataTemplate>
<TextBlock Text="{Binding ViewName}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</prism:TabControlRegionAdapter.ItemContainerStyle>
</sdk:TabControl>
Everything worked fine as long as I targeted Silverlight 4.
I got the Silverlight 5 beta and changed the project's target version to SL 5.
Now the view won't compile with error:
The property 'ItemContainerStyle' does not exist on the type 'TabControl' in the XML namespace 'http://www.codeplex.com/prism'
Anyone else got this error?
Any ideas about the causes/how to fix it?
I had the same issue. I put the tab style into the resources section of the xaml and used the following code-behind:
TabControlRegionAdapter.SetItemContainerStyle(TabControl1, Resources["TabControl1ItemStyle"] as Style);

RibbonGroupsPanel ... accepts only IProvideStarLayoutInfo instances?

I am trying to use a RibbonGallery in my application, but I get this error at runtime, when the tab which contains the gallery is loaded:
"RibbonGroupsPanel RegisterStarLayoutProvider and
UnregisterStarLayoutProvider accepts only IProvideStarLayoutInfo
instances. Parameter name: starLayoutInfoProvider"
Any idea what isn't right?
Here's the code:
<ribbon:RibbonGallery MaxColumnCount="1">
<ribbon:RibbonGalleryCategory>
<ribbon:RibbonGalleryItem Content="Green" Foreground="Green" />
<ribbon:RibbonGalleryItem Content="Blue" Foreground="Blue" />
<ribbon:RibbonGalleryItem Content="Orange" Foreground="Orange" />
</ribbon:RibbonGalleryCategory>
</ribbon:RibbonGallery>
The RibbonGallery control must be placed within a control that can take advantage of the RibbonGallery, like a RibbonSplitButton or a RibbonComboBox. Here is an example of using a gallery in a RibbonComboBox:
<ribbon:RibbonComboBox Label="1"
SmallImageSource="Images/RightArrowShort_Green16.png"
SelectionBoxWidth="62"
VerticalAlignment="Center"
IsEditable="True" >
<ribbon:RibbonGallery SelectedValue="Green"
SelectedValuePath="Content"
MaxColumnCount="1">
<ribbon:RibbonGalleryCategory>
<ribbon:RibbonGalleryItem Content="Green" Foreground="Green" />
<ribbon:RibbonGalleryItem Content="Blue" Foreground="Blue" />
<ribbon:RibbonGalleryItem Content="Orange" Foreground="Orange" />
</ribbon:RibbonGalleryCategory>
</ribbon:RibbonGallery>
</ribbon:RibbonComboBox>
XAML copied from http://msdn.microsoft.com/en-us/library/microsoft.windows.controls.ribbon.ribbongallery.aspx.
If a control is derived from RibbonMenuButton then it can contain a RibbonGallery because of the HasRibbon property.
The RibbonMenuItemsPanel-class of the System.Windows.Controls.Ribbon.Primitives allows to place a RibbonGallery in a RibbonGroup. This class implements the ISupportStarLayout-interface.
Define the primitives-Namespace in the Window-element (could also be RibbonWindow):
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell"
xmlns:primitives="clr-namespace:System.Windows.Controls.Ribbon.Primitives;assembly=System.Windows.Controls.Ribbon"
... >
The RibbonGroup-part:
<RibbonGroup Header="MyRibbonGroup">
<primitives:RibbonMenuItemsPanel Margin="0,3,0,0">
<RibbonGallery ...>
<RibbonGalleryCategory ...>
...
</RibbonGalleryCategory>
</RibbonGallery>
</primitives:RibbonMenuItemsPanel>
</RibbonGroup>
Note that i use the System.Windows.Controls.Ribbon-namespace (.Net 4.5) and not the Microsoft.Windows.Controls.Ribbon-namespace. But this should be nearly the same.
I don't see any RibbonGroupsPanel in your xaml which leads me to thinking that you're not showing all of the relevant xaml.
In any case, it tells you that you're putting the wrong element inside RibbonGroupsPanel.RegisterStarLayoutProvider and that it accepts only types that implements IProvideStarLayoutInfo .

WPF - Add Custom Font

I'm trying to add a custom font as a resource in my application.
I have a "CustomFont" directory in the application and all the fonts inside of it are set to "Resource"
<Window.Resources>
<Style x:Key="Gotham-XLight">
<Setter Property="TextElement.FontFamily"
Value="/CustomFonts;Component/#Gotham-XLight" />
</Style>
</Window.Resources>
And then on my TextBlock I have this: (inside a grid)
<TextBlock x:Name="TimeTextBlock" Style="{DynamicResource Gotham-XLight}"
TextAlignment="Center" FontSize="25" FontWeight="Bold"
Foreground="White" Text="TextBlockTimer"
Margin="105,242.974,0,226.975" HorizontalAlignment="Left"
Width="221.919" />
But I'm not seeing my font as people say. Am I doing something wrong?
You may want to check the name of the font, you need to specify the name of the font not the name of the file.
Double click on the font file and it should show a "Font name:" that's what you want to make sure is specified in your style.
Try this
<Window.Resources>
<Style x:Key="Gotham-XLight">
<Setter Property="TextElement.FontFamily" Value="CustomFonts/#Gotham-XLight" />
</Style>
</Window.Resources>
Also, if you are not planning on changing the style at runtime {StaticResource Gotham-XLight} will be much more performant.
In xaml I did it like this:
<Button Grid.Column="1" Grid.RowSpan="2" Name="start" Margin="5" Click="start_Click">
<TextBlock Name="test" FontFamily="pack://application:,,,/Y_Yoga;Component/Resources/#FontAwesome"></TextBlock>
</Button>
However, I don't know if #FontAwesome is font's embedded name or is it the result that I renamed the .ttf file.
Hope to be helpful!
Late reply but worth mentioning. To add a custom font that will apply globally in your window you could add this in your csproj file to include the fonts from the Fonts folder of your project as resources.
<ItemGroup>
<Resource Include="Fonts\*.ttf" />
</ItemGroup>
Then in your window XAML you can specify the FontFamily in the Window part:
<Window x:Class="Namespace.MainWindow"
...
FontFamily="/Fonts/#[FONT NAME]"
Title="">
<Grid>
...
</Grid>
</Window>
I hope this could help somebody, as I spent some time to figure it out.

Resources