2010-06-01 131 views
5

我的目标是通过DependencyProperties操作我的应用程序的文本样式。我得到了一个图表,其中的文字是在大小,fontfamily,颜色等操作。所以我想使用类似于丰富的文本编辑器,如Word的界面。在Combobox中显示FontFamily

我使用这个代码在我TextStyleVM http://shevaspace.blogspot.com/2006/12/i-have-some-fun-with-formattedtext_14.html

所以我有一个FontFamilyProperty和getter和setter它:

 public static DependencyProperty FontFamilyProperty = 
      DependencyProperty.Register(
       "FontFamily", 
       typeof(FontFamily), 
       typeof(OutlinedText), 
       new FrameworkPropertyMetadata(
        SystemFonts.MessageFontFamily, 
        FrameworkPropertyMetadataOptions.AffectsRender | 
        FrameworkPropertyMetadataOptions.AffectsMeasure), 
         new ValidateValueCallback(IsValidFontFamily)); 

    public FontFamily FontFamily 
    { 
     get { return (FontFamily)base.GetValue(FontFamilyProperty); } 
     set { base.SetValue(FontFamilyProperty, value); } 
    } 

然后有一个ToStyle方法,它设置样式该图的标签将被操纵:

 Style style = new Style(); 
     Binding fontFamilyBinding = new Binding("FontFamily"); 
     fontFamilyBinding.Source = this; 
     Setter fontFamilySetter = new Setter(); 
     fontFamilySetter.Property = TextBlock.FontFamilyProperty; 
     fontFamilySetter.Value = fontFamilyBinding; 
     style.Setters.Add(fontFamilySetter); 

     return style; 

现在这适用于TextBox。该文本框显示当前的FontFamily,并且如果我在文本框中输入一个新的有效的FontFamily(如Arial),则标签的FontFamily将被更改。

但是,我想要的是一个组合框,它显示SystemFonts,我可以在其中为我的标签选择一个FontFamily。但是,绑定似乎不起作用。系统字体和标签的当前字体都不显示。组合框只是空的。

这是我的XAML:

  <r:RibbonLabel Content="FontFamily" /> 
      <!--these do not work--> 
      <r:RibbonComboBox SelectedItem="{Binding FontFamily}"/> 
      <r:RibbonComboBox ItemsSource="{Binding FontFamily}"/> 
      <!--this works--> 
      <r:RibbonTextBox Text="{Binding FontFamily}"/> 

现在,我想我必须设置不同的二传手在ToStyle方法组合框。但我不知道哪一个。也许有点像这样:

  fontFamilySetter.Property = ComboBox.ItemSource; 

但是,如果我设置该属性,文本框仍然工作。那么这是否是错误的地方?如果有人能够提示我使用ToStyle方法中使用的这些Style-,Setter-,Binding-key-words的文档,我也会很感激,因为这是某人删除了我正在使用的代码。

回答

8

ItemsSource here expect a collection;例如Fonts.SystemFontFamilies

<ComboBox ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}"/> 

其实,这里有一个非常漂亮的链路覆盖字体选择:

http://www.hanselman.com/blog/LearningWPFWithBabySmashCustomerFeedbackAndAWPFFontComboBox.aspx

斯科特Hanselman的甚至展示了如何呈现与它自己的字体系列组合框每个字体项目。


根据OP评论添加。

下面是绑定到依赖项属性的示例。属性被命名为“MyFontFamily”以避免与现有Window的属性相冲突。对不起,没有丝带控制(我有裸露的3.5 SP1)。

Window1.xaml

<Window 
    x:Class="SimpleWpf.Window1" 
    x:Name="ThisWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <StackPanel Orientation="Vertical"> 
     <ComboBox 
      SelectedItem="{Binding MyFontFamily, ElementName=ThisWindow}" 
      ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}"/> 
     <TextBlock 
      Text="Lazy dog jumper" 
      FontFamily="{Binding MyFontFamily, ElementName=ThisWindow}" 
      FontSize="24"/> 
    </StackPanel> 
</Window> 

Window1.xaml。CS

public partial class Window1 : Window 
{ 
    // ... 

    public static readonly DependencyProperty MyFontFamilyProperty = 
     DependencyProperty.Register("MyFontFamily", 
     typeof(FontFamily), typeof(Window1), new UIPropertyMetadata(null)); 
} 
+0

这工作,当然,不过这并不提供任何方式修改我的FontFamilyProperty。换句话说:如何将此ComboBox绑定到名为FontFamilyProperty的DependencyProperty? 如果我将SelectedValue或SelectedItem设置为FontFamily,我会得到无效的转换异常:“System.Windows.Media.FontFamily”无法转换为“Microsoft.Windows.Controls.Ribbon.RibbonComboBoxItem” – Torsten 2010-06-08 11:01:57

+0

Hi Torsten;我已经添加了一个dp绑定的例子。除非你想做一些非常不寻常的事情,否则看起来没有任何问题。 – 2010-06-08 15:05:24

+0

感谢您的热心支持。 我将我的代码迁移到Fluent功能区,在那里您的示例工作。我在这里有一些小的变化,但它是基于你的想法: 1.我必须在SelectedItem绑定上设置Mode = TwoWay 2.我在我的DP上使用了FrameworkPropertyMetadata(请参阅shevaspace.blogspot.com中的链接我的startpost),UIPropertyMetadata也可以工作,但是,我认为。 我真的很感谢你的帮助。 – Torsten 2010-07-19 07:31:49

0

为WPF一个伟大的字体组合框可以在这里找到:

CodeProject.com: A XAML-Only Font ComboBox

它是纯粹的XAML,可以只复制/粘贴,甚至正确排序的字体。文章还很好地描述了遇到的所有问题以及如何解决它们。

2

公正在XAML解决方案按字母顺序排序的字体:

<Window x:Class="WpfFontsComboBox.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:System="clr-namespace:System;assembly=mscorlib" 
     xmlns:ComponentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase" 
     Height="350" Width="525"> 
    <Window.Resources> 
     <CollectionViewSource x:Key="SortedFontsCollection" Source="{Binding Source={x:Static Fonts.SystemFontFamilies}}" > 
      <CollectionViewSource.SortDescriptions> 
       <ComponentModel:SortDescription PropertyName="Source" /> 
      </CollectionViewSource.SortDescriptions> 
     </CollectionViewSource> 
    </Window.Resources> 
    <StackPanel> 
     <Label Content="42" FontFamily="{Binding ElementName=comboBoxFonts, Path=SelectedItem}" /> 
     <ComboBox x:Name="comboBoxFonts" ItemsSource="{Binding Source={StaticResource SortedFontsCollection}}" /> 
    </StackPanel> 
</Window> 

enter image description here enter image description here