tags:

views:

414

answers:

3

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

+4  A: 

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}"/>
Aviad P.
Yes - that's probably the most reasonable way of going about it, Thanks :)
Haemoglobin
+1  A: 

One more -

    <ComboBox
        Name="FontStretches">
        <x:Static
            Member="FontStretches.Condensed" />
        <x:Static
            Member="FontStretches.Expanded" />
        <x:Static
            Member="FontStretches.ExtraCondensed" />
        <x:Static
            Member="FontStretches.ExtraExpanded" />
        <x:Static
            Member="FontStretches.Medium" />
        <x:Static
            Member="FontStretches.Normal" />
        <x:Static
            Member="FontStretches.SemiCondensed" />
        <x:Static
            Member="FontStretches.SemiExpanded" />
        <x:Static
            Member="FontStretches.UltraCondensed" />
        <x:Static
            Member="FontStretches.UltraExpanded" />
    </ComboBox>
akjoshi
A: 

Great post! I just wanted to add something on Font Weights. The FontWeights class has all of the static properties listed above and they just encapsulate a number from 1 to 999. Some of the properties with different names encapsulate the same values, so you end up with duplicates. Plus the above example didn't have them in order. Here they are in order, duplicates removed, and comments showing the weight number for each one:

      <!--100-->
      <x:Static
        Member="FontWeights.Thin" />
      <!--200-->
      <x:Static
        Member="FontWeights.ExtraLight" />
      <!--300-->
      <x:Static
        Member="FontWeights.Light" />
      <!--400-->
      <x:Static
        Member="FontWeights.Normal" />
      <!--500-->
      <x:Static
        Member="FontWeights.Medium" />
      <!--600-->
      <x:Static
        Member="FontWeights.SemiBold" />
      <!--700-->
      <x:Static
        Member="FontWeights.Bold" />
      <!--800-->
      <x:Static
        Member="FontWeights.ExtraBold" />
      <!--900-->
      <x:Static
        Member="FontWeights.Heavy" />
      <!--950-->
      <x:Static
        Member="FontWeights.ExtraBlack" />
Sanctus