Displaying FontFamily in Combobox

后端 未结 3 1820
闹比i
闹比i 2021-02-15 17:04

My goal is to manipulate the text-styles of my application via DependencyProperties. I got a diagram in which the texts are to be manipulated in size, fontfamily, color, etc. So

相关标签:
3条回答
  • 2021-02-15 17:33

    A great Font Combobox for WPF can be found here:

    CodeProject.com: A XAML-Only Font ComboBox

    It is pure XAML, can be just copied/pasted and even sorts the fonts properly. The article also describes nicely all the gotchas encountered and how to solve them.

    0 讨论(0)
  • 2021-02-15 17:39

    A just in Xaml solution with fonts sorted in alphabetical order:

    <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

    0 讨论(0)
  • 2021-02-15 17:46

    ItemsSource here expects a collection; e.g. Fonts.SystemFontFamilies

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

    Actually, here's a very nice link covering font selection:

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

    Scott Hanselman even shows how to render each font item in combobox with it's own font family.


    Added per OP comment.

    Here's an example of binding to dependency property. Property is named "MyFontFamily" to avoid collision with existing Window's property. Sorry, no Ribbon controls (I have bare 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 讨论(0)
提交回复
热议问题