绑定到字体粗细和字体样式值列表

时间:2014-08-21 21:05:55

标签: wpf

我正在尝试绑定到WPF中定义的FontWeightsFontStyles列表。 我希望在这个帖子中使用与Displaying the list of system fonts相同的技术;但是,我无法记录Fonts结构的get_systemFontFaimlies方法,同样我找不到FontWeightsFontStyles的文档。

感谢您的帮助。

修改

虽然@ethicallogics提供的响应很有帮助,并且它可能比我的解决方案更灵活,但我只是在我的XAML中声明了数组并选择了我想要提供的FontWeightFontStyle个选项到最终用户。

我还找到了昨天找不到SystemFonts.get_systemFontFaimlies方法here的文档。遗憾的是,FontWeightsFontStyles结构似乎没有SystemFonts结构所具有的get方法,这就是为什么我必须使用项目显式定义数组的原因我想提供给最终用户。

这是我定义的数组的XAML:

<ResourceDictionary 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:media="clr-namespace:System.Windows.Media;assembly=PresentationCore"
     xmlns:win="clr-namespace:System.Windows;assembly=PresentationCore"
     xmlns:sys="clr-namespace:System;assembly=mscorlib"> 

     <ObjectDataProvider x:Key="FontFamilyOptions" ObjectType="{x:Type media:Fonts}" MethodName="get_SystemFontFamilies"/>
      <x:Array x:Key="FontWeightOptions" Type="win:FontWeight">
          <win:FontWeight>Normal</win:FontWeight>
          <win:FontWeight>Bold</win:FontWeight>
          <win:FontWeight>ExtraBold</win:FontWeight>
      </x:Array>
      <x:Array x:Key="FontStyleOptions" Type="win:FontStyle">
          <win:FontStyle>Normal</win:FontStyle>
          <win:FontStyle>Italic</win:FontStyle>
          <win:FontStyle>Oblique</win:FontStyle>
      </x:Array>
</ResourceDictionary>

2 个答案:

答案 0 :(得分:1)

创建类似

的FontWrapper类
public class FontsWrapper
{
    static ICollection<FontWeight> fontWeights;
    static ICollection<FontStyle> fontStyles;
    static ICollection<FontFamily> fontFamilies;

    public static ICollection<FontStyle> GetFontStyles()
    {
        return fontStyles ?? (fontStyles = new List<FontStyle>() { System.Windows.FontStyles.Italic, System.Windows.FontStyles.Normal, System.Windows.FontStyles.Oblique });//TODO:Get by reflection
    }

    public static ICollection<FontFamily> GetFontFamilies()
    {
        return fontFamilies ?? (fontFamilies = Fonts.SystemFontFamilies);
    }

    public static ICollection<FontWeight> GetFontWeights()
    {
        if (fontWeights == null)
            fontWeights = new List<FontWeight>();
        else
            return fontWeights;

        var type = typeof(FontWeights);
        foreach (var p in type.GetProperties().Where(s => s.PropertyType == typeof(FontWeight)))
        {
            fontWeights.Add((FontWeight)p.GetValue(null, null));
        }
        return fontWeights;
    }

    public static ICollection<FontWeight> FontWeights
    {
        get { return fontWeights ?? (fontWeights = GetFontWeights()); }
    }
    public static ICollection<FontStyle> FontStyles
    {
        get { return fontStyles ?? (fontStyles = GetFontStyles()); }
    }

    public static ICollection<FontFamily> FontFamilies
    {
        get { return fontFamilies ?? (fontFamilies = GetFontFamilies()); }
    }
}
  

XAML

<Window x:Class="WpfApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication3"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ObjectDataProvider x:Key="fontFamiliesKey" ObjectType="{x:Type local:FontsWrapper}" MethodName="GetFontFamilies"/>
    <ObjectDataProvider x:Key="fontWeightsKey" ObjectType="{x:Type local:FontsWrapper}" MethodName="GetFontStyles"/>
    <ObjectDataProvider x:Key="fontStylesKey" ObjectType="{x:Type local:FontsWrapper}" MethodName="GetFontWeights"/>
</Window.Resources>
<StackPanel>
    <ComboBox ItemsSource="{Binding Source={StaticResource fontFamiliesKey}}"></ComboBox>
    <ComboBox ItemsSource="{Binding Source={StaticResource fontWeightsKey}}"></ComboBox>
    <ComboBox ItemsSource="{Binding Source={StaticResource fontStylesKey}}"></ComboBox>
    <!-- or Bind the Lists of wrapper class Directly -->
    <ComboBox ItemsSource="{Binding Source={x:Static local:FontsWrapper.FontFamilies}}"></ComboBox>
    <ComboBox ItemsSource="{Binding Source={x:Static local:FontsWrapper.FontStyles}}"></ComboBox>
    <ComboBox ItemsSource="{Binding Source={x:Static local:FontsWrapper.FontWeights}}"></ComboBox>
</StackPanel>

我希望这会有所帮助。

答案 1 :(得分:0)

最后我在我的XAML中将数组声明为资源(带有我想提供给最终用户的选项),如下所示:

<ResourceDictionary 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:media="clr-namespace:System.Windows.Media;assembly=PresentationCore"
     xmlns:win="clr-namespace:System.Windows;assembly=PresentationCore"
     xmlns:sys="clr-namespace:System;assembly=mscorlib"> 

     <ObjectDataProvider x:Key="FontFamilyOptions" ObjectType="{x:Type media:Fonts}" MethodName="get_SystemFontFamilies"/>
      <x:Array x:Key="FontWeightOptions" Type="win:FontWeight">
          <win:FontWeight>Normal</win:FontWeight>
          <win:FontWeight>Bold</win:FontWeight>
          <win:FontWeight>ExtraBold</win:FontWeight>
      </x:Array>
      <x:Array x:Key="FontStyleOptions" Type="win:FontStyle">
          <win:FontStyle>Normal</win:FontStyle>
          <win:FontStyle>Italic</win:FontStyle>
          <win:FontStyle>Oblique</win:FontStyle>
      </x:Array>
</ResourceDictionary>

我使用这些作为我的组合框的项目来源,如下所示:

<ComboBox ItemsSource="{Binding Source={StaticResource FontFamilyOptions}}" />
<ComboBox ItemsSource="{Binding Source={StaticResource FontWeightOptions}}" />
<ComboBox ItemsSource="{Binding Source={StaticResource FontStyleOptions}}" />