A simple WPF font selector implementation and wpf font Selector

Source: Internet
Author: User

A simple WPF font selector implementation and wpf font Selector

I haven't written a blog for a long time.

This is the first blog post in the summer vacation and will be updated in the future !!!

This is a font selector I wrote. The interface is as follows:

 

The technology used in this program is relatively simple. It only uses several methods of the Font class to bind data.

First, create a Grid with four rows and two columns and add a ComboBox named fonts. Add several textblocks and a TextBox to display the font name, as shown in.

 

The complete xaml is as follows:

<Window x: Class = "WpfApplication7.MainWindow" xmlns =" http://schemas.microsoft.com/winfx/2006/xaml /Presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "Title =" font selector "Height =" 350 "Width =" 594 "Loaded =" MainWindow_Loaded_1 "> <Grid. rowDefinitions> <RowDefinition Height = "2 *"> </RowDefinition> <RowDefinition Height = "2 *"> </RowDefinition> <RowDefinition Height = "2 *"> </RowDefinition> <RowDefinition Height = "4 *"> </RowDefinition> </Grid. rowDefinitions> <Grid. columnDefinitions> <ColumnDefinition> </Grid. columnDefinitions> <ComboBox HorizontalAlignment = "Center" MinWidth = "200" Name = "fonts" SelectionChanged = "fonts_SelectionChanged" Grid. columnSpan = "2" MinHeight = "30" Margin = "20"> </ComboBox> <TextBlock Grid. row = "1"> Chinese preview: </TextBlock> <TextBlock Grid. row = "2" Name = "text" FontSize = "30"> the font you select </TextBlock> <TextBlock Grid. row = "1" Grid. column = "2"> English preview: </TextBlock> <TextBlock Grid. row = "2" Grid. column = "2" FontSize = "30" FontFamily = "{Binding Path = FontFamily, ElementName = text}"> You Selected Font </TextBlock> <Border Grid. columnSpan = "2" Grid. row = "3"> <DockPanel> <TextBlock DockPanel. dock = "Top"> font details: </TextBlock> <StackPanel Orientation = "Horizontal"> <TextBlock> Font Name: </TextBlock> <TextBox Text = "{Binding ElementName = text, path = FontFamily} "Name =" fontname "> </TextBox> </StackPanel> <Button HorizontalAlignment =" Center "verticalignment =" Bottom "DockPanel. dock = "Bottom" Click = "Button_Click_1"> <Button. style> <Style. triggers> <Trigger Property = "Button. isMouseOver "Value =" True "> <Setter Property =" Button. foreground "Value =" Blue "> </Setter> </Trigger> <Trigger Property =" Button. isMouseOver "Value =" False "> <Setter Property =" Button. foreground "Value =" Black "> </Setter> </Trigger> </Style. triggers> </Style> </Button. style> <Button. template> <ControlTemplate> <TextBlock> <Underline> follow my Sina Weibo </Underline> </TextBlock> </ControlTemplate> </Button. template> </Button> </DockPanel> </Border> </Grid> </Window>

 

Element Binding is used, for example, FontFamily = "{Binding Path = FontFamily, ElementName = text}">. ElementName indicates the Name (Name attribute) of the element to be bound ), path indicates the name of the property to be bound.

Here we use the Fonts. SystemFontFamilies set to obtain the Fonts installed in the system, and then use the foreach loop to add the Fonts to the Items set of ComboBox.

Window load Event code:

Foreach (FontFamily font in Fonts. SystemFontFamilies) {fonts. Items. Add (font. Source);/* Ask hovertree.com */}

 

After running the command, you will find that the names of all fonts are displayed in ComboBox. However, the font fonts cannot be displayed in the list. We can use custom controls to solve this problem.

Create a WPF control (named Items), put a TextBlock control in it, name it text, and type the following code in the constructor:

Public Items (FontFamily font)
{
InitializeComponent ();
Text. FontFamily = font;
Text. Text = font. Source;

}

A parameter is added to accept the FontFamily object and set TextBlock content and FontFamily as the passed parameter.

The Code is as follows:

Foreach (FontFamily font in Fonts. systemFontFamilies) // traverses the Chinese-German font of the font set {/* He asked hovertree.com */fonts. items. add (new Items (font); // PASS Parameters to custom controls}

 

In this case, OK.

Recommended: http://www.cnblogs.com/roucheng/category/827769.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.