Click here to Skip to main content
15,907,231 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

i have a program that creates a TextBlock at runtime. i have added a font to a subfolder named "Fonts" in the project. i want to give that font to the TextBlocks i have made at runtime. i have written this code :

TextBlock t = new TextBlock();
t.Text = names[i];
t.FontFamily = new FontFamily("pack://application:,,,/MyProjects;component/Fonts/#IranNastaliq");


when i create a TextBlock in visual studio and blend and change the text font to a added font in the project they generate different codes which none of them works in the code above.here are the codes that they generate :

visual studio's code :
"Fonts/Iran%20Nastaliq.ttf#IranNastaliq"


Blend's code :
"/MyProject;component/#IranNastaliq"


Any way to solve this problem?

Thanks
Posted

As far as I can see, according to this[^], what you are doing should work. The font does not load at all ?
 
Share this answer
 
Comments
M-Shaf 12-Jul-11 3:29am    
thanks; but i had seen that site before and tested it and it didn't work.

once i tested one of the ways it worked but after that whenever i ran the program it didn't work.isn't there any problem in build options or something else?
Hi
After playing with it i found that when visual studio or blend generates your textblock family font it sets its fontfamily baseUri property to what it should be. actually all of the above sources should work but you have to set a FontFamily.baseUri to the textblock fontfamilys baseUri:

"pack://application:,,,/MyProject;component/mainwindow.xaml"

where instead of MyProject - as you know - you should put your projects name and instead of mainwindow.xaml you should put your files name (My file was MainWindow so i think its not case sensitive in this situation).

sorry for my English!
Hope it helped.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900