开发者

Binding FontStyles and FontWeights to WPF ComboBox

I was just wondering if it would be possible to bind th开发者_开发百科e 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


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}"/>


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>


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.

Ref: - FontWeights Class

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" />


And the last one, I guess:

<ComboBox Name="FontSizes">
  <ComboBoxItem Content="8" HorizontalContentAlignment="Right"/>
  <ComboBoxItem Content="9" HorizontalContentAlignment="Right"/>
  <ComboBoxItem Content="10" HorizontalContentAlignment="Right"/>
  <ComboBoxItem Content="11" HorizontalContentAlignment="Right"/>
  <ComboBoxItem Content="12" HorizontalContentAlignment="Right"/>
  <ComboBoxItem Content="14" HorizontalContentAlignment="Right"/>
  <ComboBoxItem Content="16" HorizontalContentAlignment="Right"/>
  <ComboBoxItem Content="18" HorizontalContentAlignment="Right"/>
  <ComboBoxItem Content="20" HorizontalContentAlignment="Right"/>
  <ComboBoxItem Content="22" HorizontalContentAlignment="Right"/>
  <ComboBoxItem Content="24" HorizontalContentAlignment="Right"/>
  <ComboBoxItem Content="26" HorizontalContentAlignment="Right"/>
  <ComboBoxItem Content="28" HorizontalContentAlignment="Right"/>
  <ComboBoxItem Content="36" HorizontalContentAlignment="Right"/>
  <ComboBoxItem Content="48" HorizontalContentAlignment="Right"/>
  <ComboBoxItem Content="72" HorizontalContentAlignment="Right"/>
</ComboBox>

I searched the internet all over, but could not find out how to get the standard list of font sizes. Seems we have to hardcode it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜