Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding FontStyles and FontWeights to WPF ComboBox

I was just wondering if it would be possible to bind the list of available FontStyles and FontWeights to a ComboBox?

For example, to bind the list of fonts to a combobox you can use:

FontComboBox.ItemsSource = Fonts.SystemFontFamilies;

Can I also have something for :

FontStyleComboBox.ItemsSource = .... 
FontWeightComboBox.ItemsSource = ....  ?

Would it require reflection on the System.Windows.FontWeights and System.Windows.FontStyles classes or would there be an easier way than that?

Thanks

like image 627
Haemoglobin Avatar asked Jan 03 '10 11:01

Haemoglobin


1 Answers

For the font families combo:

<ComboBox Name="Families" ItemsSource="{x:Static Fonts.SystemFontFamilies}"/>

For the font styles:

<ComboBox Name="Styles">
    <x:Static Member="FontStyles.Normal"/>
    <x:Static Member="FontStyles.Italic"/>
    <x:Static Member="FontStyles.Oblique"/>
</ComboBox>

And for the font weights:

<ComboBox Name="Weights">
    <x:Static Member="FontWeights.Black"/>
    <x:Static Member="FontWeights.Bold"/>
    <x:Static Member="FontWeights.DemiBold"/>
    <x:Static Member="FontWeights.ExtraBlack"/>
    <x:Static Member="FontWeights.ExtraBold"/>
    <x:Static Member="FontWeights.ExtraLight"/>
    <x:Static Member="FontWeights.Heavy"/>
    <x:Static Member="FontWeights.Light"/>
    <x:Static Member="FontWeights.Medium"/>
    <x:Static Member="FontWeights.Normal"/>
    <x:Static Member="FontWeights.Regular"/>
    <x:Static Member="FontWeights.SemiBold"/>
    <x:Static Member="FontWeights.Thin"/>
    <x:Static Member="FontWeights.UltraBlack"/>
    <x:Static Member="FontWeights.UltraBold"/>
    <x:Static Member="FontWeights.UltraLight"/>
</ComboBox>

And now to test:

<TextBlock 
    Text="This is some text." 
    FontFamily="{Binding ElementName=Families, Path=SelectedItem}" 
    FontStyle="{Binding ElementName=Styles, Path=SelectedItem}" 
    FontWeight="{Binding ElementName=Weights, Path=SelectedItem}"/>
like image 185
Aviad P. Avatar answered Sep 25 '22 02:09

Aviad P.