none
How Set font family in C# wpf not in xaml RRS feed

  • Question

  • Hi,first of all I am sorry for my language grammar because my first language is persian(Iran)

    I added 2 custom font into my project at folder(folder name is Font Collection) by create folder then drag and drop font

    I have no problem when I use XAML and this is my code FontFamily="Font Collection/Roboto.ttf#Roboto"

    But at xaml.cs(C#) i want to change this font, for example Suppose in window load event i want change this

    I cant change it i search link below

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.media.fontfamily?view=net-5.0

    But it did not work

    my code is

    FontFamily = new FontFamily(new Uri(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "/Font Collection/")), "/#Bahij Uthman Taha");




    • Edited by reza_jafery Sunday, November 22, 2020 7:41 PM
    Sunday, November 22, 2020 7:38 PM

Answers

  • Hooray Hooray

    I found the solution myself

    I suggest two solutions

    The first solution

    First add your folder package font in startup folder (Bin/Debug) this solution makes your font portable on any system

    Then i try lot of code and the following codes are solution

    FontFamily = new FontFamily(System.Environment.CurrentDirectory + @"\Font Collection\Bahij Uthman Taha.ttf#Bahij Uthman Taha");

    Or(Both codes do the same thing)

    FontFamily = new FontFamily(System.Environment.CurrentDirectory + "\\Font Collection\\Bahij Uthman Taha.ttf#Bahij Uthman Taha");

    Attention!!!!: Font Collection is my folder name and Bahij Uthman Taha.ttf is my font name you can change These

    The second solution

    First add your folder package font in Solution Explorer (AT visual studio Solution Explorer window)

    FontFamily = new FontFamily(new Uri( App.Current.StartupUri.ToString()),"\\Font Collection\\#Bahij Uthman Taha");

    Or(Both codes do the same thing)

    FontFamily = new FontFamily(new Uri( App.Current.StartupUri.ToString()),"Font Collection/#Bahij Uthman Taha");

    Attention!!!!: Font Collection is my folder name and Bahij Uthman Taha.ttf is my font name you can change These

    I tested both 1000% working

    Please share my solution

    Thanks








    • Marked as answer by reza_jafery Wednesday, November 25, 2020 8:25 PM
    • Unmarked as answer by reza_jafery Friday, November 27, 2020 7:03 PM
    • Marked as answer by reza_jafery Friday, November 27, 2020 7:28 PM
    • Unmarked as answer by reza_jafery Friday, November 27, 2020 7:29 PM
    • Edited by reza_jafery Friday, November 27, 2020 7:29 PM
    • Marked as answer by reza_jafery Friday, November 27, 2020 7:30 PM
    Monday, November 23, 2020 10:25 PM

All replies

  • Hello,

    See if the following article/code sample might help.

    https://gallery.technet.microsoft.com/WPF-Dynamic-Fonts-ad3741ca


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Sunday, November 22, 2020 10:34 PM
    Moderator
  • Hi,

    Thank you for posting here.

    I successfully modified fontfamily using the method in the document.

    One is to ensure that this font does exist;

    Second, this font seems to be very similar to the default font. Is it possible that it has been changed but cannot be seen?

    Try to use other fonts with large differences in style, or use the code to get the font after modification to see if it has changed.

    Best Regards,

    Timon


    "Visual C#" forum will be migrating to a new home on Microsoft Q&A ! We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A ! For more information, please refer to the sticky post.

    Monday, November 23, 2020 2:45 AM
  • Unfortunately, your help did not work
    Monday, November 23, 2020 5:50 PM
  • Unfortunately, your help did not work
    Monday, November 23, 2020 5:50 PM
  • I cant access to font with uri address , how do I enter the address?

    I test code below at XAML and i have no problem

    FontFamily="Font Collection/Roboto.ttf#Roboto"

    but

    I test code below at c# and didn't work

    FontFamily = new FontFamily(new Uri(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "/Font Collection/")), "/#Bahij Uthman Taha");





    • Edited by reza_jafery Monday, November 23, 2020 7:02 PM
    Monday, November 23, 2020 6:56 PM
  • Hooray Hooray

    I found the solution myself

    I suggest two solutions

    The first solution

    First add your folder package font in startup folder (Bin/Debug) this solution makes your font portable on any system

    Then i try lot of code and the following codes are solution

    FontFamily = new FontFamily(System.Environment.CurrentDirectory + @"\Font Collection\Bahij Uthman Taha.ttf#Bahij Uthman Taha");

    Or(Both codes do the same thing)

    FontFamily = new FontFamily(System.Environment.CurrentDirectory + "\\Font Collection\\Bahij Uthman Taha.ttf#Bahij Uthman Taha");

    Attention!!!!: Font Collection is my folder name and Bahij Uthman Taha.ttf is my font name you can change These

    The second solution

    First add your folder package font in Solution Explorer (AT visual studio Solution Explorer window)

    FontFamily = new FontFamily(new Uri( App.Current.StartupUri.ToString()),"\\Font Collection\\#Bahij Uthman Taha");

    Or(Both codes do the same thing)

    FontFamily = new FontFamily(new Uri( App.Current.StartupUri.ToString()),"Font Collection/#Bahij Uthman Taha");

    Attention!!!!: Font Collection is my folder name and Bahij Uthman Taha.ttf is my font name you can change These

    I tested both 1000% working

    Please share my solution

    Thanks








    • Marked as answer by reza_jafery Wednesday, November 25, 2020 8:25 PM
    • Unmarked as answer by reza_jafery Friday, November 27, 2020 7:03 PM
    • Marked as answer by reza_jafery Friday, November 27, 2020 7:28 PM
    • Unmarked as answer by reza_jafery Friday, November 27, 2020 7:29 PM
    • Edited by reza_jafery Friday, November 27, 2020 7:29 PM
    • Marked as answer by reza_jafery Friday, November 27, 2020 7:30 PM
    Monday, November 23, 2020 10:25 PM
  • Hi reza_jafery,

    I'm glad to hear that.

    Please mark your own reply as an answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    "Visual C#" forum will be migrating to a new home on Microsoft Q&A ! We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A ! For more information, please refer to the sticky post

    Tuesday, November 24, 2020 1:21 AM