How to set the default fontfamily manually for WPF application?

William Liu 466 Reputation points
2024-08-03T02:31:04.0866667+00:00

I am doing MVVM FontFamily binding for my WPF application, which user can choose FontFamily and set for the application.

When my application runs the first time, the selection FontFamily is null. I have no idea how to set the WPF application to default font when the SelectedFont is null.

I tried to look into the Window FontFamilyProperty and found the default value is SystemFonts.MessageFontFamily. But when I create and run an empty default WPF application, I can see the default of Window is Yahei, not as same as SystemFonts.MessageFontFamily.

Could you help me to find out how to set the fontFamily to default (something like Yahei based on the environment) when the SelectedFontFamily is null? Thanks!

VM:

public class MainViewModel
{
    public FontFamily? SelectedFontFamily { get; set; }

    // ... other code
}

View:

<Window
    x:Class="FontTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:fontTest="clr-namespace:FontTest"
    Width="800"
    Height="450"
    FontFamily="{Binding SelectedFontFamily, TargetNullValue=SystemFonts.MessageFontFamily}">
  
    <Window.DataContext>
        <fontTest:MainViewModel />
    </Window.DataContext>

    <TextBlock Text="Hello world" />
</Window>

Env: Windows10 19044 Simplified Chinese .net 8 v8.0.303

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,781 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,546 Reputation points Microsoft Vendor
    2024-08-05T09:02:53.52+00:00

    Hi,@William Liu . Welcome to Microsoft Q&A. For MVVM FontFamily binding and WPF application user-selectable FontFamily issues, you could try the following solutions.

    Method1:

    
     <Window.DataContext>
         <local:MainViewModel/>
     </Window.DataContext>
      <StackPanel Orientation="Horizontal">
          <ComboBox Height="20" x:Name="FontFamilyComboBox" SelectedIndex="0"   SelectedValue="{Binding SelectedFontFamily,UpdateSourceTrigger=PropertyChanged}"   ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}"   Width="120">
          </ComboBox>
      <TextBox  TextWrapping="Wrap" x:Name="TextBoxInsertText" Width="300" Text="hello ,test" FontFamily="{Binding ElementName=FontFamilyComboBox, Path=SelectedItem}" 
        AcceptsReturn="True" AcceptsTab="True" AllowDrop="True" IsUndoEnabled="True"/>
    </StackPanel>
    
    

    MainViewModel :

    
     public class MainViewModel : INotifyPropertyChanged
     {
      
         private System.Windows.Media.FontFamily selectedfontfamily;
         public System.Windows.Media.FontFamily SelectedFontFamily
         {
             get
             {
                 return this.selectedfontfamily;
             }
             set
             {
                 this.selectedfontfamily = value;
                 this.PropertyChanged(this, new PropertyChangedEventArgs("SelectedFontFamily"));
             }
         }
    
         public event PropertyChangedEventHandler? PropertyChanged;
    
         protected virtual void OnPropertyChanged(string propertyName)
         {
             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
         }
     }
    

    Method2:

     <Window.Resources>
         <local:FontFamilyConversions x:Key="FontFamilyConversions" />
     </Window.Resources>
    
     <StackPanel Orientation="Horizontal">
      
         <ComboBox  DockPanel.Dock="Top" x:Name="Fonttype"  SelectedIndex="0">
             <ComboBoxItem >Arial</ComboBoxItem>
             <ComboBoxItem>Batang</ComboBoxItem>
             <ComboBoxItem>BatangChe</ComboBoxItem>
             <ComboBoxItem>Gungsuh</ComboBoxItem>
             <ComboBoxItem>GungsuhChe</ComboBoxItem>
             <ComboBoxItem>Courier New</ComboBoxItem>
         </ComboBox>
         <TextBox x:Name="editor" FontSize="16" Height="59" Text="Hello,test" FontFamily="{Binding Path=SelectedValue, ElementName=Fonttype, Mode=OneWay, Converter={StaticResource FontFamilyConversions}}" />
     
     </StackPanel>
    
    

    FontFamilyConversions:

     public class FontFamilyConversions : IValueConverter
     {
         public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
         {
             FontFamily fontfamily = new FontFamily("Verdana");
             ComboBoxItem selectedFont = value as ComboBoxItem;
             if (selectedFont != null)
             {
                 fontfamily = new FontFamily(selectedFont.Content.ToString());
             }
             return fontfamily;
         }
         public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
         {
             return null;
         }
     }
    

    Method3:

    
    
      <StackPanel Orientation="Horizontal">
    
          <ComboBox  DockPanel.Dock="Top" x:Name="Fonttype" ItemsSource="{x:Static Fonts.SystemFontFamilies}" SelectedIndex="0"  Height="59"/>
    
    
          <TextBox x:Name="editor1" FontSize="16" Height="59" Text="Hello,test" FontFamily="{Binding Path=SelectedValue, ElementName=Fonttype, Mode=OneWay}" />
    
      </StackPanel>
    
    
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.