using font file in wpf usercontrol - wpf

I am using a WPF UserControl inside a Winforms form. I have it working.
I would like the text that I use in the WPF UserControl to use a font that I have as a TTF.
I do not know how to reference this TTF and have the control use it. I am assuming I should load the TTF (this is not an installed font) in the UserControl and tell the control (a label) to use it but I only see reference to the font-family.

Here is how to load a font in wpf from a TTF font file
<TextBlock FontSize="48" FontFamily="/Assets/Fonts/Algeria.TTF#Algeria" FontWeight="Normal">test value</TextBlock>
Use a Resource to store the font...

Make sure the font name is correct ....
from git hub I found the following font
github.com/mozilla/Fira/blob/master/ttf/FiraSans-Medium.ttf. For this font the font would be fira Sans Medium so the Xaml would look like
<Label FontFamily="/Resources/firaSans-Medium.ttf#fira Sans Medium" x:Name="TopText" Content="Memory Disk Registry System Program" FontSize="3" Margin="0,-2,0,0"/>

Related

Elementhost: Images in XAML Ribbon Causing Issues

I am using WPF Interoperability (ElementHost) to display a XAML control in WinForms. This works perfectly for my needs as I must use WinForms for a project I am working on. The issue is that when I add images, such as icons, to the Ribbon in XAML, I can no longer add the XAML Ribbon to the WinForm. The error I keep getting is:
An error occurred trying to create an object of type 'WPF.Ribbon'. Make sure the type has a default constructor.
The images have a Build Action of "Resource". Here is an example XAML code snippet I am using for my ribbon code:
<RibbonButton SmallImageSource="/App/Images/Ribbon/cut.png" Label="Cut" KeyTip="X" />
When I remove the image source, I can compile the code fine, and the ribbon displays without any issue.
The resolution is to add absolute addresses, such as:
<RibbonButton SmallImageSource = "C:\Path To Images\App\Images\Ribbon\cut.png" Label="Cut" KeyTip="X" />

Import a new font into WPF application MVVM

I have file 'resource.xaml' for all the styles. But I need to add a new font file to my project and re-use it instead of default font. How can I add a new font and use it?
I searched the internet and see some methods. but nothing worked for me.
Thanks in Advance.
when using a custom font you must respect the following syntax:
"/FontPath/FontFileName.ttf#FontName"
for exemple :
<Setter Property="FontFamily" Value="/Fonts/VLADIMIR.TTF#Vladimir Script"/>
where the name of this font is "Vladimir Script".
I've had this problem before, and here's how I did it:
Create a folder in your solution and add all of the font .ttf files in there.
In App.xaml, or a resource dictionary, add the following code:
pack://application:,,,/Path/To/Font/#Name Of Font
Then, you can reference the font resource in your XAML code:
<TextBlock FontFamily="{DynamicResource NameOfResource}" Text="Hello World"/>

absolute URI to font in resource

I would like use custom font in my WPF app.
I created folder fonts in my project and added .ttf file. Then I setup build action for .ttf file to resource.
When I access to font in XAML via absolute URI it doesnt’t work.
<TextBlock Margin = "5"
FontSize = "50"
FontFamily="pack://application:,,,/fonts/Sketch College.ttf">
Custom font
</TextBlock>
Where is problem?
Because if I use relative URI it works.
<TextBlock Margin = "5"
FontSize = "40"
FontFamily = "./fonts/#Sketch College">
Custom fonts
</TextBlock>
Thanks
WPF applications do not allow you to create a FontFamily object programmatically using "pack:" as part of the absolute uniform resource identifier (URI) reference to a font. For example, "pack://application:,,,/resources/#Pericles Light" is an invalid font reference.
Got it from
http://msdn.microsoft.com/en-us/library/ms753303(v=vs.110).aspx

Finding a Control e.g. Label and assign its Content at runtime mvvm wpf

We have one xaml file in our Skins folder where designer has developed some styles for the labels,radiobuttons or checkboxes. I need to find the control, Label in this xaml file and assign the content property at runtime in virewModel so that it can automatically displays the content as per the records in the database. How to achieve this.
Kindly Suggest?
Thanks.
Why not just have the Label's Content property bound to your view model:
<Label Content="{Binding CustomerName}"/>

Using a System.Drawing.Font with a WPF Label

I have a WPF Label control which I'm trying to change the appearance of using a System.Drawing.Font object supplied by some legacy code. I have been able to set most of the properties, but am struggling with Strikeout and Underline.
So far I have:
System.Drawing.Font font = FontFromLegacyCode();
System.Windows.Controls.Label label = new System.Windows.Controls.Label();
label.FontFamily = new System.Windows.Media.FontFamily( font.Name );
label.FontWeight = font.Bold ? System.Windows.FontWeights.Bold : System.Windows.FontWeights.Regular;
label.FontStyle = font.Italic ? System.Windows.FontStyles.Italic : System.Windows.FontStyles.Normal;
label.FontSize = font.Size;
How do you set the font strikeout or underline properties? Is there a better control to use?
I would change it to a TextBlock control. The TextBlock control has the TextDecorations property you can use.
<TextBlock Name="textBlock" TextDecorations="Underline, Strikethrough" />
Or you can stick a TextBlock inside a Label if you really like (although I'd just use the TextBlock by itself).
<Label Name="label">
<TextBlock Name="textBlock" TextDecorations="Underline, Strikethrough" />
</Label>
Have a look at the TextDecorations class.
I find that TextBlocks are more suitable than Labels in most situations. Here's a blog post about the differences. The chief difference being that a Label is a Control whereas a TextBlock is just a FrameworkElement. Also a Label supports access keys.
Looking at the code you already have, there might be a problem with it.
On the MSDN on Windows Form and WPF Property mapping they make the comment:
Font size in WPF is expressed as one ninety-sixth of an inch, and in Windows Forms as one seventy-second of an inch. The corresponding conversion is:
Windows Forms font size = WPF font size * 72.0 / 96.0.

Resources