2010-06-01 129 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