├── .gitattributes
├── .gitignore
├── LICENSE
├── README.md
├── TestProject
├── App.xaml
├── App.xaml.cs
├── MainWindow.xaml
├── MainWindow.xaml.cs
└── TestProject.csproj
├── WpfColorFontDialog.sln
└── WpfColorFontDialog
├── AvailableColors.cs
├── ColorFontChooser.xaml
├── ColorFontChooser.xaml.cs
├── ColorFontDialog.xaml
├── ColorFontDialog.xaml.cs
├── ColorPicker.xaml
├── ColorPicker.xaml.cs
├── ColorPickerViewModel.cs
├── FontColor.cs
├── FontInfo.cs
├── FontSizeListBoxItemToDoubleConverter.cs
├── FontValueConverter.cs
├── I18NUtil.cs
├── I18n
├── en-US.xaml
└── zh-CN.xaml
├── WpfColorFontDialog.csproj
└── resources
└── colorfont_icon.png
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.sln.docstates
8 |
9 | # Build results
10 | [Dd]ebug/
11 | [Dd]ebugPublic/
12 | [Rr]elease/
13 | x64/
14 | build/
15 | bld/
16 | [Bb]in/
17 | [Oo]bj/
18 |
19 | # Visual Studio 2015 cache/options directory
20 | .vs/
21 |
22 | # Roslyn cache directories
23 | *.ide/
24 |
25 | # MSTest test Results
26 | [Tt]est[Rr]esult*/
27 | [Bb]uild[Ll]og.*
28 |
29 | #NUNIT
30 | *.VisualState.xml
31 | TestResult.xml
32 |
33 | # Build Results of an ATL Project
34 | [Dd]ebugPS/
35 | [Rr]eleasePS/
36 | dlldata.c
37 |
38 | *_i.c
39 | *_p.c
40 | *_i.h
41 | *.ilk
42 | *.meta
43 | *.obj
44 | *.pch
45 | *.pdb
46 | *.pgc
47 | *.pgd
48 | *.rsp
49 | *.sbr
50 | *.tlb
51 | *.tli
52 | *.tlh
53 | *.tmp
54 | *.tmp_proj
55 | *.log
56 | *.vspscc
57 | *.vssscc
58 | .builds
59 | *.pidb
60 | *.svclog
61 | *.scc
62 |
63 | # Chutzpah Test files
64 | _Chutzpah*
65 |
66 | # Visual C++ cache files
67 | ipch/
68 | *.aps
69 | *.ncb
70 | *.opensdf
71 | *.sdf
72 | *.cachefile
73 |
74 | # Visual Studio profiler
75 | *.psess
76 | *.vsp
77 | *.vspx
78 |
79 | # TFS 2012 Local Workspace
80 | $tf/
81 |
82 | # Guidance Automation Toolkit
83 | *.gpState
84 |
85 | # ReSharper is a .NET coding add-in
86 | _ReSharper*/
87 | *.[Rr]e[Ss]harper
88 | *.DotSettings.user
89 |
90 | # JustCode is a .NET coding addin-in
91 | .JustCode
92 |
93 | # TeamCity is a build add-in
94 | _TeamCity*
95 |
96 | # DotCover is a Code Coverage Tool
97 | *.dotCover
98 |
99 | # NCrunch
100 | _NCrunch_*
101 | .*crunch*.local.xml
102 |
103 | # MightyMoose
104 | *.mm.*
105 | AutoTest.Net/
106 |
107 | # Web workbench (sass)
108 | .sass-cache/
109 |
110 | # Installshield output folder
111 | [Ee]xpress/
112 |
113 | # DocProject is a documentation generator add-in
114 | DocProject/buildhelp/
115 | DocProject/Help/*.HxT
116 | DocProject/Help/*.HxC
117 | DocProject/Help/*.hhc
118 | DocProject/Help/*.hhk
119 | DocProject/Help/*.hhp
120 | DocProject/Help/Html2
121 | DocProject/Help/html
122 |
123 | # Click-Once directory
124 | publish/
125 |
126 | # Publish Web Output
127 | *.[Pp]ublish.xml
128 | *.azurePubxml
129 | ## TODO: Comment the next line if you want to checkin your
130 | ## web deploy settings but do note that will include unencrypted
131 | ## passwords
132 | #*.pubxml
133 |
134 | # NuGet Packages Directory
135 | packages/*
136 | ## TODO: If the tool you use requires repositories.config
137 | ## uncomment the next line
138 | #!packages/repositories.config
139 |
140 | # Enable "build/" folder in the NuGet Packages folder since
141 | # NuGet packages use it for MSBuild targets.
142 | # This line needs to be after the ignore of the build folder
143 | # (and the packages folder if the line above has been uncommented)
144 | !packages/build/
145 |
146 | # Windows Azure Build Output
147 | csx/
148 | *.build.csdef
149 |
150 | # Windows Store app package directory
151 | AppPackages/
152 |
153 | # Others
154 | sql/
155 | *.Cache
156 | ClientBin/
157 | [Ss]tyle[Cc]op.*
158 | ~$*
159 | *~
160 | *.dbmdl
161 | *.dbproj.schemaview
162 | *.pfx
163 | *.publishsettings
164 | node_modules/
165 |
166 | # RIA/Silverlight projects
167 | Generated_Code/
168 |
169 | # Backup & report files from converting an old project file
170 | # to a newer Visual Studio version. Backup files are not needed,
171 | # because we have git ;-)
172 | _UpgradeReport_Files/
173 | Backup*/
174 | UpgradeLog*.XML
175 | UpgradeLog*.htm
176 |
177 | # SQL Server files
178 | *.mdf
179 | *.ldf
180 |
181 | # Business Intelligence projects
182 | *.rdl.data
183 | *.bim.layout
184 | *.bim_*.settings
185 |
186 | # Microsoft Fakes
187 | FakesAssemblies/
188 |
189 | # LightSwitch generated files
190 | GeneratedArtifacts/
191 | _Pvt_Extensions/
192 | ModelManifest.xml
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Sverre Skodje
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Wpf Color/Font Dialog
2 |
3 | A wpf color and font picker based on [a project by Alessio Saltarin](http://www.codeproject.com/Articles/368070/A-WPF-Font-Picker-with-Color).
4 |
5 | Available on [NuGet](http://www.nuget.org/packages/WpfColorFontDialog/)
6 |
7 | usage:
8 |
9 | //We can pass a bool to choose if we preview the font directly in the list of fonts.
10 | Bool previewFontInFontList = true;
11 | //True to allow user to input arbitrary font sizes. False to only allow predtermined sizes
12 | Bool allowArbitraryFontSizes = true;
13 |
14 |
15 | ColorFontDialog dialog = new ColorFontDialog(previewFontInFontList,allowArbitraryFontSizes);
16 | dialog.Font = FontInfo.GetControlFont(MyTextBox);
17 |
18 | //Optional custom allowed size range
19 | dialog.FontSizes = new int[] { 10, 12, 14, 16, 18, 20, 22 };
20 |
21 | if (dialog.ShowDialog() == true)
22 | {
23 | FontInfo font = dialog.Font;
24 | if (font != null)
25 | {
26 | FontInfo.ApplyFont(MyTextBox, font);
27 | }
28 | }
29 |
30 | 
31 |
32 |
--------------------------------------------------------------------------------
/TestProject/App.xaml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/TestProject/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Configuration;
4 | using System.Data;
5 | using System.Linq;
6 | using System.Windows;
7 |
8 | namespace TestProject
9 | {
10 | ///
11 | /// Interaction logic for App.xaml
12 | ///
13 | public partial class App : Application
14 | {
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/TestProject/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
11 |
17 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/TestProject/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Windows;
6 | using System.Windows.Controls;
7 | using System.Windows.Data;
8 | using System.Windows.Documents;
9 | using System.Windows.Input;
10 | using System.Windows.Media;
11 | using System.Windows.Media.Imaging;
12 | using System.Windows.Navigation;
13 | using System.Windows.Shapes;
14 | using WpfColorFontDialog;
15 |
16 | namespace TestProject
17 | {
18 | ///
19 | /// Interaction logic for MainWindow.xaml
20 | ///
21 | public partial class MainWindow : Window
22 | {
23 | public MainWindow()
24 | {
25 | InitializeComponent();
26 | }
27 |
28 | private void Button_Click(object sender, RoutedEventArgs e)
29 | {
30 | ColorFontDialog dialog = new ColorFontDialog(true,true,true);
31 |
32 | dialog.Owner = this;
33 | dialog.Font = FontInfo.GetControlFont(this.TextBlockSample);
34 | //dialog.FontSizes = new int[] { 10, 12, 14, 16, 18 };
35 | if (dialog.ShowDialog() == true)
36 | {
37 | FontInfo font = dialog.Font;
38 | if (font != null)
39 | {
40 | FontInfo.ApplyFont(this.TextBlockSample, font);
41 | }
42 | }
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/TestProject/TestProject.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | WinExe
5 | net40;netcoreapp3.1
6 | true
7 | latest
8 | TestProject
9 | TestProject
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/WpfColorFontDialog.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2013
4 | VisualStudioVersion = 12.0.31101.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfColorFontDialog", "WpfColorFontDialog\WpfColorFontDialog.csproj", "{6C301B62-352A-4053-9B06-E148A85D47AA}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProject", "TestProject\TestProject.csproj", "{782ECF2F-4758-4292-8594-AF401511FFB1}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Release|Any CPU = Release|Any CPU
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {6C301B62-352A-4053-9B06-E148A85D47AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17 | {6C301B62-352A-4053-9B06-E148A85D47AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
18 | {6C301B62-352A-4053-9B06-E148A85D47AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
19 | {6C301B62-352A-4053-9B06-E148A85D47AA}.Release|Any CPU.Build.0 = Release|Any CPU
20 | {782ECF2F-4758-4292-8594-AF401511FFB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {782ECF2F-4758-4292-8594-AF401511FFB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {782ECF2F-4758-4292-8594-AF401511FFB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {782ECF2F-4758-4292-8594-AF401511FFB1}.Release|Any CPU.Build.0 = Release|Any CPU
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | EndGlobal
29 |
--------------------------------------------------------------------------------
/WpfColorFontDialog/AvailableColors.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Reflection;
4 | using System.Windows.Media;
5 |
6 | namespace WpfColorFontDialog
7 | {
8 | internal class AvailableColors : List
9 | {
10 | public AvailableColors()
11 | {
12 | this.Init();
13 | }
14 |
15 | public static FontColor GetFontColor(SolidColorBrush b)
16 | {
17 | return (new AvailableColors()).GetFontColorByBrush(b);
18 | }
19 |
20 | public static FontColor GetFontColor(string name)
21 | {
22 | return (new AvailableColors()).GetFontColorByName(name);
23 | }
24 |
25 | public static FontColor GetFontColor(Color c)
26 | {
27 | return AvailableColors.GetFontColor(new SolidColorBrush(c));
28 | }
29 |
30 | public FontColor GetFontColorByBrush(SolidColorBrush b)
31 | {
32 | FontColor found = null;
33 | foreach (FontColor brush in this)
34 | {
35 | if (!brush.Brush.Color.Equals(b.Color))
36 | {
37 | continue;
38 | }
39 | found = brush;
40 | break;
41 | }
42 | return found;
43 | }
44 |
45 | public FontColor GetFontColorByName(string name)
46 | {
47 | FontColor found = null;
48 | foreach (FontColor b in this)
49 | {
50 | if (b.Name != name)
51 | {
52 | continue;
53 | }
54 | found = b;
55 | break;
56 | }
57 | return found;
58 | }
59 |
60 | public static int GetFontColorIndex(FontColor c)
61 | {
62 | AvailableColors brushList = new AvailableColors();
63 | int idx = 0;
64 | SolidColorBrush colorBrush = c.Brush;
65 | foreach (FontColor brush in brushList)
66 | {
67 | if (brush.Brush.Color.Equals(colorBrush.Color))
68 | {
69 | break;
70 | }
71 | idx++;
72 | }
73 | return idx;
74 | }
75 |
76 | private void Init()
77 | {
78 | PropertyInfo[] properties = typeof(Colors).GetProperties(BindingFlags.Static | BindingFlags.Public);
79 | for (int i = 0; i < (int)properties.Length; i++)
80 | {
81 | PropertyInfo prop = properties[i];
82 | string name = prop.Name;
83 | SolidColorBrush brush = new SolidColorBrush((Color)prop.GetValue(null, null));
84 | base.Add(new FontColor(name, brush));
85 | }
86 | }
87 | }
88 | }
--------------------------------------------------------------------------------
/WpfColorFontDialog/ColorFontChooser.xaml:
--------------------------------------------------------------------------------
1 |
3 |
14 |
15 |
16 |
17 |
21 |
22 |
23 |
25 |
26 |
28 |
29 |
31 |
32 |
34 |
35 |
36 |
38 |
39 |
41 |
42 |
43 |
44 |
45 |
46 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
73 |
74 | LightBlue
75 |
76 |
77 |
78 |
79 |
80 |
81 |
83 |
84 |
87 |
88 | LightBlue
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
104 |
105 | LightBlue
106 |
107 |
108 |
116 |
117 |
118 |
122 |
123 |
125 |
126 |
141 |
142 |
146 |
147 |
149 |
150 |
154 |
155 |
156 |
157 |
158 |
--------------------------------------------------------------------------------
/WpfColorFontDialog/ColorFontChooser.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Windows;
6 | using System.Windows.Controls;
7 | using System.Windows.Data;
8 | using System.Windows.Documents;
9 | using System.Windows.Input;
10 | using System.Windows.Media;
11 | using System.Windows.Media.Imaging;
12 | using System.Windows.Navigation;
13 | using System.Windows.Shapes;
14 |
15 | namespace WpfColorFontDialog
16 | {
17 | ///
18 | /// Interaction logic for ColorFontChooser.xaml
19 | ///
20 | public partial class ColorFontChooser : UserControl
21 | {
22 | public FontInfo SelectedFont
23 | {
24 | get
25 | {
26 | return new FontInfo(this.txtSampleText.FontFamily, this.txtSampleText.FontSize, this.txtSampleText.FontStyle, this.txtSampleText.FontStretch, this.txtSampleText.FontWeight, this.colorPicker.SelectedColor.Brush);
27 | }
28 | }
29 |
30 |
31 |
32 | public bool ShowColorPicker
33 | {
34 | get { return (bool)GetValue(ShowColorPickerProperty); }
35 | set { SetValue(ShowColorPickerProperty, value); }
36 | }
37 |
38 | // Using a DependencyProperty as the backing store for ShowColorPicker. This enables animation, styling, binding, etc...
39 | public static readonly DependencyProperty ShowColorPickerProperty =
40 | DependencyProperty.Register("ShowColorPicker", typeof(bool), typeof(ColorFontChooser), new PropertyMetadata(true, ShowColorPickerPropertyCallback));
41 |
42 |
43 | public bool AllowArbitraryFontSizes
44 | {
45 | get { return (bool)GetValue(AllowArbitraryFontSizesProperty); }
46 | set { SetValue(AllowArbitraryFontSizesProperty, value); }
47 | }
48 |
49 | // Using a DependencyProperty as the backing store for AllowArbitraryFontSizes. This enables animation, styling, binding, etc...
50 | public static readonly DependencyProperty AllowArbitraryFontSizesProperty =
51 | DependencyProperty.Register("AllowArbitraryFontSizes", typeof(bool), typeof(ColorFontChooser), new PropertyMetadata(true, AllowArbitraryFontSizesPropertyCallback));
52 |
53 |
54 | public bool PreviewFontInFontList
55 | {
56 | get { return (bool)GetValue(PreviewFontInFontListProperty); }
57 | set { SetValue(PreviewFontInFontListProperty, value); }
58 | }
59 |
60 | // Using a DependencyProperty as the backing store for PreviewFontInFontList. This enables animation, styling, binding, etc...
61 | public static readonly DependencyProperty PreviewFontInFontListProperty =
62 | DependencyProperty.Register("PreviewFontInFontList", typeof(bool), typeof(ColorFontChooser), new PropertyMetadata(true, PreviewFontInFontListPropertyCallback));
63 |
64 |
65 | public ColorFontChooser()
66 | {
67 | InitializeComponent();
68 | this.groupBoxColorPicker.Visibility = ShowColorPicker ? Visibility.Visible : Visibility.Collapsed;
69 | this.tbFontSize.IsEnabled = AllowArbitraryFontSizes;
70 | lstFamily.ItemTemplate = PreviewFontInFontList ? (DataTemplate)Resources["fontFamilyData"] : (DataTemplate)Resources["fontFamilyDataWithoutPreview"];
71 | }
72 | private static void PreviewFontInFontListPropertyCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
73 | {
74 | ColorFontChooser chooser = d as ColorFontChooser;
75 | if (e.NewValue == null)
76 | return;
77 | if ((bool)e.NewValue == true)
78 | chooser.lstFamily.ItemTemplate = chooser.Resources["fontFamilyData"] as DataTemplate;
79 | else
80 | chooser.lstFamily.ItemTemplate = chooser.Resources["fontFamilyDataWithoutPreview"] as DataTemplate;
81 | }
82 | private static void AllowArbitraryFontSizesPropertyCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
83 | {
84 | ColorFontChooser chooser = d as ColorFontChooser;
85 | if (e.NewValue == null)
86 | return;
87 |
88 | chooser.tbFontSize.IsEnabled = (bool)e.NewValue;
89 |
90 | }
91 | private static void ShowColorPickerPropertyCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
92 | {
93 | ColorFontChooser chooser = d as ColorFontChooser;
94 | if (e.NewValue == null)
95 | return;
96 | if ((bool)e.NewValue == true)
97 | chooser.groupBoxColorPicker.Visibility = Visibility.Visible;
98 | else
99 | chooser.groupBoxColorPicker.Visibility = Visibility.Collapsed;
100 | }
101 |
102 | private void colorPicker_ColorChanged(object sender, RoutedEventArgs e)
103 | {
104 | this.txtSampleText.Foreground = this.colorPicker.SelectedColor.Brush;
105 | }
106 |
107 | private void lstFontSizes_SelectionChanged(object sender, SelectionChangedEventArgs e)
108 | {
109 | if (this.tbFontSize != null && this.lstFontSizes.SelectedItem != null)
110 | {
111 | this.tbFontSize.Text = this.lstFontSizes.SelectedItem.ToString();
112 | }
113 | }
114 |
115 | private void tbFontSize_PreviewTextInput(object sender, TextCompositionEventArgs e)
116 | {
117 | e.Handled = !TextBoxTextAllowed(e.Text);
118 | }
119 |
120 | private void tbFontSize_Pasting(object sender, DataObjectPastingEventArgs e)
121 | {
122 | if (e.DataObject.GetDataPresent(typeof(String)))
123 | {
124 | String Text1 = (String)e.DataObject.GetData(typeof(String));
125 | if (!TextBoxTextAllowed(Text1)) e.CancelCommand();
126 | }
127 | else
128 | {
129 | e.CancelCommand();
130 | }
131 | }
132 | private Boolean TextBoxTextAllowed(String Text2)
133 | {
134 | return Array.TrueForAll(Text2.ToCharArray(),
135 | delegate (Char c) { return Char.IsDigit(c) || Char.IsControl(c); });
136 | }
137 |
138 | private void tbFontSize_LostFocus(object sender, RoutedEventArgs e)
139 | {
140 | if (tbFontSize.Text.Length == 0)
141 | {
142 | if (this.lstFontSizes.SelectedItem == null)
143 | {
144 | lstFontSizes.SelectedIndex = 0;
145 | }
146 | tbFontSize.Text = this.lstFontSizes.SelectedItem.ToString();
147 |
148 | }
149 | }
150 |
151 | private void tbFontSize_TextChanged(object sender, TextChangedEventArgs e)
152 | {
153 | foreach (int size in this.lstFontSizes.Items)
154 | {
155 | if (size.ToString() == tbFontSize.Text)
156 | {
157 | lstFontSizes.SelectedItem = size;
158 | lstFontSizes.ScrollIntoView(size);
159 | return;
160 | }
161 | }
162 | this.lstFontSizes.SelectedItem = null;
163 | }
164 | }
165 | }
166 |
--------------------------------------------------------------------------------
/WpfColorFontDialog/ColorFontDialog.xaml:
--------------------------------------------------------------------------------
1 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
29 |
30 |
31 |
32 |
33 |
34 |
38 |
44 |
48 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/WpfColorFontDialog/ColorFontDialog.xaml.cs:
--------------------------------------------------------------------------------
1 | using System.Collections;
2 | using System.Windows;
3 | using System.Windows.Media;
4 |
5 | namespace WpfColorFontDialog
6 | {
7 | ///
8 | /// Interaction logic for ColorFontDialog.xaml
9 | ///
10 | public partial class ColorFontDialog : Window
11 | {
12 | private FontInfo _selectedFont;
13 |
14 | public FontInfo Font
15 | {
16 | get
17 | {
18 | return _selectedFont;
19 | }
20 | set
21 | {
22 | _selectedFont = value;
23 | }
24 | }
25 |
26 | private int[] _defaultFontSizes = { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72, 96 };
27 | private int[] _fontSizes = null;
28 | public int[] FontSizes
29 | {
30 | get
31 | {
32 | return _fontSizes ?? _defaultFontSizes;
33 | }
34 | set
35 | {
36 | _fontSizes = value;
37 | }
38 | }
39 | public ColorFontDialog(bool previewFontInFontList = true, bool allowArbitraryFontSizes = true, bool showColorPicker = true)
40 | {
41 | InitializeComponent();
42 | I18NUtil.SetLanguage(Resources);
43 | this.colorFontChooser.PreviewFontInFontList = previewFontInFontList;
44 | this.colorFontChooser.AllowArbitraryFontSizes = allowArbitraryFontSizes;
45 | this.colorFontChooser.ShowColorPicker = showColorPicker;
46 | }
47 |
48 | private void btnOk_Click(object sender, RoutedEventArgs e)
49 | {
50 | this.Font = this.colorFontChooser.SelectedFont;
51 | base.DialogResult = new bool?(true);
52 | }
53 |
54 | private void SyncFontColor()
55 | {
56 | int colorIdx = AvailableColors.GetFontColorIndex(this.Font.Color);
57 | this.colorFontChooser.colorPicker.superCombo.SelectedIndex = colorIdx;
58 | this.colorFontChooser.txtSampleText.Foreground = this.Font.Color.Brush;
59 | this.colorFontChooser.colorPicker.superCombo.BringIntoView();
60 | }
61 |
62 | private void SyncFontName()
63 | {
64 | string fontFamilyName = this._selectedFont.Family.Source;
65 | bool foundMatch = false;
66 | int idx = 0;
67 | foreach (object item in (IEnumerable)this.colorFontChooser.lstFamily.Items)
68 | {
69 | if (fontFamilyName == item.ToString())
70 | {
71 | foundMatch = true;
72 | break;
73 | }
74 | idx++;
75 | }
76 | if (!foundMatch)
77 | {
78 | idx = 0;
79 | }
80 | this.colorFontChooser.lstFamily.SelectedIndex = idx;
81 | this.colorFontChooser.lstFamily.ScrollIntoView(this.colorFontChooser.lstFamily.Items[idx]);
82 | }
83 |
84 | private void SyncFontSize()
85 | {
86 | double fontSize = this._selectedFont.Size;
87 | this.colorFontChooser.lstFontSizes.ItemsSource = FontSizes;
88 | this.colorFontChooser.tbFontSize.Text = fontSize.ToString();
89 | }
90 |
91 | private void SyncFontTypeface()
92 | {
93 | string fontTypeFaceSb = FontInfo.TypefaceToString(this._selectedFont.Typeface);
94 | int idx = 0;
95 | foreach (object item in (IEnumerable)this.colorFontChooser.lstTypefaces.Items)
96 | {
97 | if (fontTypeFaceSb == FontInfo.TypefaceToString(item as FamilyTypeface))
98 | {
99 | break;
100 | }
101 | idx++;
102 | }
103 | this.colorFontChooser.lstTypefaces.SelectedIndex = idx;
104 | this.colorFontChooser.lstTypefaces.ScrollIntoView(this.colorFontChooser.lstTypefaces.SelectedItem);
105 | }
106 |
107 | private void Window_Loaded_1(object sender, RoutedEventArgs e)
108 | {
109 | this.SyncFontColor();
110 | this.SyncFontName();
111 | this.SyncFontSize();
112 | this.SyncFontTypeface();
113 | }
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/WpfColorFontDialog/ColorPicker.xaml:
--------------------------------------------------------------------------------
1 |
3 |
10 |
11 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
33 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/WpfColorFontDialog/ColorPicker.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Windows;
6 | using System.Windows.Controls;
7 | using System.Windows.Data;
8 | using System.Windows.Documents;
9 | using System.Windows.Input;
10 | using System.Windows.Media;
11 | using System.Windows.Media.Imaging;
12 | using System.Windows.Navigation;
13 | using System.Windows.Shapes;
14 |
15 | namespace WpfColorFontDialog
16 | {
17 | ///
18 | /// Interaction logic for ColorPicker.xaml
19 | ///
20 | public partial class ColorPicker : UserControl
21 | {
22 | private ColorPickerViewModel viewModel;
23 |
24 | public readonly static RoutedEvent ColorChangedEvent;
25 |
26 | public readonly static DependencyProperty SelectedColorProperty;
27 |
28 | public FontColor SelectedColor
29 | {
30 | get
31 | {
32 | FontColor fc = (FontColor)base.GetValue(ColorPicker.SelectedColorProperty) ?? AvailableColors.GetFontColor("Black");
33 | return fc;
34 | }
35 | set
36 | {
37 | this.viewModel.SelectedFontColor = value;
38 | base.SetValue(ColorPicker.SelectedColorProperty, value);
39 | }
40 | }
41 |
42 | static ColorPicker()
43 | {
44 | ColorPicker.ColorChangedEvent = EventManager.RegisterRoutedEvent("ColorChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ColorPicker));
45 | ColorPicker.SelectedColorProperty = DependencyProperty.Register("SelectedColor", typeof(FontColor), typeof(ColorPicker), new UIPropertyMetadata(null));
46 | }
47 | public ColorPicker()
48 | {
49 | InitializeComponent();
50 | this.viewModel = new ColorPickerViewModel();
51 | base.DataContext = this.viewModel;
52 | }
53 | private void RaiseColorChangedEvent()
54 | {
55 | base.RaiseEvent(new RoutedEventArgs(ColorPicker.ColorChangedEvent));
56 | }
57 |
58 | private void superCombo_DropDownClosed(object sender, EventArgs e)
59 | {
60 | base.SetValue(ColorPicker.SelectedColorProperty, this.viewModel.SelectedFontColor);
61 | this.RaiseColorChangedEvent();
62 | }
63 |
64 | private void superCombo_Loaded(object sender, RoutedEventArgs e)
65 | {
66 | base.SetValue(ColorPicker.SelectedColorProperty, this.viewModel.SelectedFontColor);
67 | }
68 |
69 | public event RoutedEventHandler ColorChanged
70 | {
71 | add
72 | {
73 | base.AddHandler(ColorPicker.ColorChangedEvent, value);
74 | }
75 | remove
76 | {
77 | base.RemoveHandler(ColorPicker.ColorChangedEvent, value);
78 | }
79 | }
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/WpfColorFontDialog/ColorPickerViewModel.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.ObjectModel;
3 | using System.ComponentModel;
4 | using System.Threading;
5 | using System.Windows.Media;
6 |
7 | namespace WpfColorFontDialog
8 | {
9 | internal class ColorPickerViewModel : INotifyPropertyChanged
10 | {
11 | private ReadOnlyCollection roFontColors;
12 |
13 | private FontColor selectedFontColor;
14 |
15 | public ReadOnlyCollection FontColors
16 | {
17 | get
18 | {
19 | return this.roFontColors;
20 | }
21 | }
22 |
23 | public FontColor SelectedFontColor
24 | {
25 | get
26 | {
27 | return this.selectedFontColor;
28 | }
29 | set
30 | {
31 | if (this.selectedFontColor == value)
32 | {
33 | return;
34 | }
35 | this.selectedFontColor = value;
36 | this.OnPropertyChanged("SelectedFontColor");
37 | }
38 | }
39 |
40 | public ColorPickerViewModel()
41 | {
42 | this.selectedFontColor = AvailableColors.GetFontColor(Colors.Black);
43 | this.roFontColors = new ReadOnlyCollection(new AvailableColors());
44 | }
45 |
46 | private void OnPropertyChanged(string propertyName)
47 | {
48 | if (this.PropertyChanged != null)
49 | {
50 | this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
51 | }
52 | }
53 |
54 | public event PropertyChangedEventHandler PropertyChanged;
55 | }
56 | }
--------------------------------------------------------------------------------
/WpfColorFontDialog/FontColor.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.CompilerServices;
3 | using System.Windows;
4 | using System.Windows.Media;
5 |
6 | namespace WpfColorFontDialog
7 | {
8 | public class FontColor
9 | {
10 | public SolidColorBrush Brush
11 | {
12 | get;
13 | set;
14 | }
15 |
16 | public string Name
17 | {
18 | get;
19 | set;
20 | }
21 |
22 | public FontColor(string name, SolidColorBrush brush)
23 | {
24 | this.Name = name;
25 | this.Brush = brush;
26 | }
27 |
28 | public override bool Equals(object obj)
29 | {
30 | if (obj == null)
31 | {
32 | return false;
33 | }
34 | FontColor p = obj as FontColor;
35 | if (p == null)
36 | {
37 | return false;
38 | }
39 | if (this.Name != p.Name)
40 | {
41 | return false;
42 | }
43 | return this.Brush.Equals(p.Brush);
44 | }
45 |
46 | public bool Equals(FontColor p)
47 | {
48 | if (p == null)
49 | {
50 | return false;
51 | }
52 | if (this.Name != p.Name)
53 | {
54 | return false;
55 | }
56 | return this.Brush.Equals(p.Brush);
57 | }
58 |
59 | public override int GetHashCode()
60 | {
61 | return base.GetHashCode();
62 | }
63 |
64 | public override string ToString()
65 | {
66 | string[] name = new string[] { "FontColor [Color=", this.Name, ", ", this.Brush.ToString(), "]" };
67 | return string.Concat(name);
68 | }
69 | }
70 | }
--------------------------------------------------------------------------------
/WpfColorFontDialog/FontInfo.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.CompilerServices;
3 | using System.Text;
4 | using System.Windows;
5 | using System.Windows.Controls;
6 | using System.Windows.Media;
7 |
8 | namespace WpfColorFontDialog
9 | {
10 | public class FontInfo
11 | {
12 | public SolidColorBrush BrushColor
13 | {
14 | get;
15 | set;
16 | }
17 |
18 | public FontColor Color
19 | {
20 | get
21 | {
22 | return AvailableColors.GetFontColor(this.BrushColor);
23 | }
24 | }
25 |
26 | public FontFamily Family
27 | {
28 | get;
29 | set;
30 | }
31 |
32 | public double Size
33 | {
34 | get;
35 | set;
36 | }
37 |
38 | public FontStretch Stretch
39 | {
40 | get;
41 | set;
42 | }
43 |
44 | public FontStyle Style
45 | {
46 | get;
47 | set;
48 | }
49 |
50 | public FamilyTypeface Typeface
51 | {
52 | get
53 | {
54 | FamilyTypeface ftf = new FamilyTypeface()
55 | {
56 | Stretch = this.Stretch,
57 | Weight = this.Weight,
58 | Style = this.Style
59 | };
60 | return ftf;
61 | }
62 | }
63 |
64 | public FontWeight Weight
65 | {
66 | get;
67 | set;
68 | }
69 |
70 | public FontInfo()
71 | {
72 | }
73 |
74 | public FontInfo(FontFamily fam, double sz, FontStyle style, FontStretch strc, FontWeight weight, SolidColorBrush c)
75 | {
76 | this.Family = fam;
77 | this.Size = sz;
78 | this.Style = style;
79 | this.Stretch = strc;
80 | this.Weight = weight;
81 | this.BrushColor = c;
82 | }
83 |
84 | public static void ApplyFont(Control control, FontInfo font)
85 | {
86 | control.FontFamily = font.Family;
87 | control.FontSize = font.Size;
88 | control.FontStyle = font.Style;
89 | control.FontStretch = font.Stretch;
90 | control.FontWeight = font.Weight;
91 | control.Foreground = font.BrushColor;
92 | }
93 |
94 | public static FontInfo GetControlFont(Control control)
95 | {
96 | FontInfo font = new FontInfo()
97 | {
98 | Family = control.FontFamily,
99 | Size = control.FontSize,
100 | Style = control.FontStyle,
101 | Stretch = control.FontStretch,
102 | Weight = control.FontWeight,
103 | BrushColor = (SolidColorBrush)control.Foreground
104 | };
105 | return font;
106 | }
107 |
108 | public static string TypefaceToString(FamilyTypeface ttf)
109 | {
110 | StringBuilder sb = new StringBuilder(ttf.Stretch.ToString());
111 | sb.Append("-");
112 | sb.Append(ttf.Weight.ToString());
113 | sb.Append("-");
114 | sb.Append(ttf.Style.ToString());
115 | return sb.ToString();
116 | }
117 | }
118 | }
--------------------------------------------------------------------------------
/WpfColorFontDialog/FontSizeListBoxItemToDoubleConverter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Globalization;
3 | using System.Windows.Controls;
4 | using System.Windows.Data;
5 |
6 | namespace WpfColorFontDialog
7 | {
8 | public class FontSizeListBoxItemToDoubleConverter : IValueConverter
9 | {
10 | public FontSizeListBoxItemToDoubleConverter()
11 | {
12 | }
13 |
14 | object System.Windows.Data.IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
15 | {
16 | string str = value.ToString();
17 | try
18 | {
19 | return double.Parse(value.ToString());
20 | }
21 | catch(FormatException ex)
22 | {
23 | return 0;
24 | }
25 |
26 | }
27 |
28 | object System.Windows.Data.IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
29 | {
30 | throw new NotImplementedException();
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/WpfColorFontDialog/FontValueConverter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Globalization;
3 | using System.Windows.Data;
4 | using System.Windows.Markup;
5 | using System.Windows.Media;
6 |
7 | namespace WpfColorFontDialog
8 | {
9 | public class FontValueConverter : IValueConverter
10 | {
11 | public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
12 | {
13 | if (value is FontFamily font)
14 | {
15 | var name = I18NUtil.CurrentLanguage;
16 | if (string.IsNullOrWhiteSpace(name))
17 | {
18 | name = I18NUtil.GetCurrentLanguage();
19 | }
20 | var names = new[] { name, I18NUtil.GetLanguage() };
21 | foreach (var s in names)
22 | {
23 | if (font.FamilyNames.TryGetValue(XmlLanguage.GetLanguage(s), out var localizedName))
24 | {
25 | if (!string.IsNullOrEmpty(localizedName))
26 | {
27 | return localizedName;
28 | }
29 | }
30 | }
31 |
32 | return font.Source;
33 | }
34 | throw new NotSupportedException();
35 | }
36 |
37 | public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
38 | {
39 | throw new NotSupportedException("ConvertBack not supported");
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/WpfColorFontDialog/I18NUtil.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Windows;
5 |
6 | namespace WpfColorFontDialog
7 | {
8 | internal static class I18NUtil
9 | {
10 | private const string DefaultLanguage = @"en-US";
11 |
12 | public static string CurrentLanguage;
13 |
14 | public static readonly Dictionary SupportLanguage = new Dictionary
15 | {
16 | {@"简体中文", @"zh-CN"},
17 | {@"English (United States)", @"en-US"},
18 | };
19 |
20 | public static string GetCurrentLanguage()
21 | {
22 | return System.Globalization.CultureInfo.CurrentCulture.Name;
23 | }
24 |
25 | public static string GetLanguage()
26 | {
27 | var name = GetCurrentLanguage();
28 | return SupportLanguage.Any(s => name == s.Value) ? name : DefaultLanguage;
29 | }
30 |
31 | public static string GetWindowStringValue(Window window, string key)
32 | {
33 | if (window.Resources.MergedDictionaries[0][key] is string str)
34 | {
35 | return str;
36 | }
37 | return null;
38 | }
39 |
40 | public static void SetLanguage(ResourceDictionary resources, string langName = @"")
41 | {
42 | if (string.IsNullOrEmpty(langName))
43 | {
44 | langName = GetLanguage();
45 | }
46 | CurrentLanguage = langName;
47 | if (resources.MergedDictionaries.Count > 0)
48 | {
49 | resources.MergedDictionaries[0].Source = new Uri($@"I18n/{langName}.xaml", UriKind.Relative);
50 | }
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/WpfColorFontDialog/I18n/en-US.xaml:
--------------------------------------------------------------------------------
1 |
4 | en-US
5 |
6 | Font Family
7 | Style - Weight
8 | Font Size
9 | Sample Text
10 | Font Color
11 |
12 | Select Font
13 | OK
14 | Cancel
15 |
16 |
--------------------------------------------------------------------------------
/WpfColorFontDialog/I18n/zh-CN.xaml:
--------------------------------------------------------------------------------
1 |
4 | zh-CN
5 |
6 | 字体
7 | 字形
8 | 大小
9 | 示例
10 | 颜色
11 |
12 | 选择字体
13 | 确定
14 | 取消
15 |
16 |
--------------------------------------------------------------------------------
/WpfColorFontDialog/WpfColorFontDialog.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net40;netcoreapp3.1
5 | true
6 | latest
7 | WpfColorFontDialog
8 | WpfColorFontDialog
9 | Copyright © 2015
10 | 1.0.8.0
11 | true
12 | Added .NET Core 3.1 support.
13 | https://github.com/sskodje/WpfColorFont/
14 | https://github.com/sskodje/WpfColorFont/
15 | A wpf color and font picker.
16 | Sverre Kristoffer Skodje
17 |
18 | wpf font dialog color
19 | MIT
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/WpfColorFontDialog/resources/colorfont_icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sskodje/WpfColorFont/aee5c28351e49f831d374628cb242a5ac69fdd2f/WpfColorFontDialog/resources/colorfont_icon.png
--------------------------------------------------------------------------------