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.