Recently, I wrote a simple text editor, which naturally requires the font and color selection function. Of course, the font and color here are limited to the simple selector in ComboBox or other similar list properties, instead of the font/color/dialog/picker control. Let's first look at the system font selection.
Font selection
In WPF, The system. Windows. Media. fonts class can return system font information (including fontfamily and typeface). We can use it to define data directly.
Template, but pay attention to the following:
- The default value of itemspanel in ComboBox is stackpanel rather than virtualizingstackpanel. In this way, opening ComboBox is very slow when there are many system fonts. You need to set it to virtualizingstackpanel, which greatly improves the performance.
- The issynchronizedwithcurrentitem of ComboBox is true, so the current option of binding the textblock to collectionviewsource is exactly the choice of ComboBox.
Result Preview
Code:
<Window. Resources>
<Collectionviewsource X: Key = "sysfonts" Source = "{X: static fonts. systemfontfamilies}"/>
</Window. Resources>
<Grid>
<Grid. rowdefinitions>
<Rowdefinition Height = "Auto"> </rowdefinition>
<Rowdefinition> </rowdefinition>
<Rowdefinition Height = "Auto"> </rowdefinition>
</Grid. rowdefinitions>
<ComboBox name = "cbx"
Itemssource = "{binding source = {staticresource sysfonts }}"
Issynchronizedwithcurrentitem = "true">
<ComboBox. itemspanel>
<Itemspaneltemplate>
<Virtualizingstackpanel> </virtualizingstackpanel>
</Itemspaneltemplate>
</ComboBox. itemspanel>
<ComboBox. itemtemplate>
<Datatemplate>
<Textblock text = "{binding}" fontfamily = "{binding}" fontsize = "16"> </textblock>
</Datatemplate>
</ComboBox. itemtemplate>
</ComboBox>
<Stackpanel grid. Row = "2" horizontalalignment = "center">
<Textblock text = "preview:"> </textblock>
<Textblock text = "Liu Yuanyuan 123 mgen"
Fontsize = "20"
Fontfamily = "{binding source = {staticresource sysfonts}, Path =/}">
</Textblock>
</Stackpanel>
</GRID>
Color Selection
When you select a color, you can predefine several color values and add them to the list. Then, add a "more colors" button to call some external colorpicker to allow the user to select other non-predefined colors. But I always think this method is not very good. At least it is not natural. You still need to select the color by yourself. Finally, one day you can see the answer on stackoverflow.com. Address: http://stackoverflow.com/questions/562682/how-can-i-list-colors-in-wpf-with-xaml
The solution is the answer to that question.
The principle is to use the member attribute names of the colors class (or the brushes class) to enumerate colors, and use two objectdataprovider to call type. GetType and type. getproperties gradually to obtain all propertyinfo of the colors class.
However, the answer to the original question is to display only the name, and we will slightly modify the definition data.
Template to display the color. Thanks to the ubiquitous typeconverter of WPF, The propertyinfo. name will be converted to the corresponding color.
Run preview:
Code (also pure XAML)
<! -- This code is modified on other original code -->
<! -- Original code: http://stackoverflow.com/questions/562682/how-can-i-list-colors-in-wpf-with-xaml -->
<Window. Resources>
<Objectdataprovider methodname = "GetType"
Objecttype = "{X: Type SYS: Type}" X: Key = "colorstypeodp">
<Objectdataprovider. methodparameters>
<SYS: String> system. Windows. Media. colors, presentationcore,
Version = 3.0.0.0, culture = neutral,
Publickeytoken = 31bf3856ad364e35 </sys: String>
</Objectdataprovider. methodparameters>
</Objectdataprovider>
<Objectdataprovider objectinstance = "{staticresource colorstypeodp }"
Methodname = "getproperties" X: Key = "colorpropertiesodp">
</Objectdataprovider>
</Window. Resources>
<Stackpanel>
<ComboBox name = "combobox1" horizontalcontentalignment = "stretch"
Itemssource = "{binding source = {staticresource colorpropertiesodp}">
<ComboBox. itemtemplate>
<Datatemplate>
<Stackpanel orientation = "horizontal">
<Rectangle fill = "{binding name}" margin = "1 3" Height = "10" width = "100"> </rectangle>
<Textblock text = "{binding name}" margin = "2 0"> </textblock>
</Stackpanel>
</Datatemplate>
</ComboBox. itemtemplate>
</ComboBox>
</Stackpanel>