├── example.gif ├── src ├── CurrencyTextBoxExample │ ├── app.config │ ├── Properties │ │ ├── Settings.settings │ │ ├── Settings.Designer.cs │ │ ├── AssemblyInfo.cs │ │ ├── Resources.Designer.cs │ │ └── Resources.resx │ ├── App.xaml │ ├── App.xaml.cs │ ├── MainWindow.xaml.cs │ ├── MainWindow.xaml │ └── CurrencyTextBoxExample.csproj ├── CurrencyTextBoxControl │ ├── Properties │ │ ├── Settings.settings │ │ ├── Settings.Designer.cs │ │ ├── AssemblyInfo.cs │ │ ├── Resources.Designer.cs │ │ └── Resources.resx │ ├── Themes │ │ └── Generic.xaml │ ├── CurrencyTextBoxControl.csproj │ └── CurrencyTextBox.cs └── CurrencyTextBoxControl.sln ├── README.md ├── LICENSE └── .gitignore /example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mtusk/wpf-currency-textbox/HEAD/example.gif -------------------------------------------------------------------------------- /src/CurrencyTextBoxExample/app.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxControl/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxExample/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxExample/App.xaml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxExample/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 CurrencyTextBoxExample 9 | { 10 | /// 11 | /// Interaction logic for App.xaml 12 | /// 13 | public partial class App : Application 14 | { 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WPF Currency TextBox 2 | A WPF TextBox for entering a currency value, similar to how a cash register works. 3 | 4 | ![example](example.gif?raw=true) 5 | 6 | ## Features 7 | - Numbers typed are pushed in from the right. If we start with the default value 0.00, and start typing the numbers 123, the value updates as such: 0.00 => 0.01 => 0.12 => 1.23 8 | - If we press the backspace key, the numbers are shifted right: 1.23 => 0.12 => 0.01 => 0.00 9 | - If we press the delete key, the value is reset to 0.00. 10 | - If we press the minus key, the value becomes negative. 11 | - Copy and paste are disabled (both via context menu and keyboard shortcuts) 12 | - This control's template can be customized to change the appearance. 13 | - Supports data validation. 14 | 15 | ## How to use 16 | Add a reference to `CurrencyTextBoxControl.dll` from your project, then add the following namespace to your XAML: 17 | 18 | ```xaml 19 | xmlns:currency="clr-namespace:CurrencyTextBoxControl;assembly=CurrencyTextBoxControl" 20 | ``` 21 | 22 | Insert the control like this: 23 | 24 | ```xaml 25 | 26 | ``` 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Adam Anderson 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 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxControl/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.225 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace CurrencyTextBoxControl.Properties { 12 | 13 | 14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")] 16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { 17 | 18 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 19 | 20 | public static Settings Default { 21 | get { 22 | return defaultInstance; 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxExample/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.225 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace CurrencyTextBoxExample.Properties { 12 | 13 | 14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")] 16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { 17 | 18 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 19 | 20 | public static Settings Default { 21 | get { 22 | return defaultInstance; 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxExample/MainWindow.xaml.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.ComponentModel; 3 | using System.Windows; 4 | 5 | namespace CurrencyTextBoxExample 6 | { 7 | public partial class MainWindow : Window, IDataErrorInfo 8 | { 9 | private decimal _number = 1.23M; 10 | public decimal Number 11 | { 12 | get 13 | { 14 | return _number; 15 | } 16 | set 17 | { 18 | _number = value; 19 | } 20 | } 21 | 22 | private List _stringFormats; 23 | public List StringFormats 24 | { 25 | get 26 | { 27 | if (_stringFormats == null) 28 | { 29 | _stringFormats = new List() { "C", "E", "F", "G", "N", "P", "{0:C1}", "{0:C0}" }; 30 | } 31 | 32 | return _stringFormats; 33 | } 34 | set 35 | { 36 | _stringFormats = value; 37 | } 38 | } 39 | 40 | public MainWindow() 41 | { 42 | InitializeComponent(); 43 | 44 | this.DataContext = this; 45 | } 46 | 47 | public string Error 48 | { 49 | get { throw new System.NotImplementedException(); } 50 | } 51 | 52 | public string this[string columnName] 53 | { 54 | get 55 | { 56 | if (columnName == "Number" && 57 | (_number < 0 || _number > 10)) 58 | { 59 | return "Number must be between zero and ten."; 60 | } 61 | else 62 | { 63 | return null; 64 | } 65 | } 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxControl/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Resources; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | using System.Windows; 6 | 7 | // General Information about an assembly is controlled through the following 8 | // set of attributes. Change these attribute values to modify the information 9 | // associated with an assembly. 10 | [assembly: AssemblyTitle("CurrencyTextBoxControl")] 11 | [assembly: AssemblyDescription("")] 12 | [assembly: AssemblyConfiguration("")] 13 | [assembly: AssemblyCompany("")] 14 | [assembly: AssemblyProduct("CurrencyTextBoxControl")] 15 | [assembly: AssemblyCopyright("")] 16 | [assembly: AssemblyTrademark("")] 17 | [assembly: AssemblyCulture("")] 18 | 19 | // Setting ComVisible to false makes the types in this assembly not visible 20 | // to COM components. If you need to access a type in this assembly from 21 | // COM, set the ComVisible attribute to true on that type. 22 | [assembly: ComVisible(false)] 23 | 24 | //In order to begin building localizable applications, set 25 | //CultureYouAreCodingWith in your .csproj file 26 | //inside a . For example, if you are using US english 27 | //in your source files, set the to en-US. Then uncomment 28 | //the NeutralResourceLanguage attribute below. Update the "en-US" in 29 | //the line below to match the UICulture setting in the project file. 30 | 31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] 32 | 33 | 34 | [assembly: ThemeInfo( 35 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located 36 | //(used if a resource is not found in the page, 37 | // or application resource dictionaries) 38 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located 39 | //(used if a resource is not found in the page, 40 | // app, or any theme specific resource dictionaries) 41 | )] 42 | 43 | 44 | // Version information for an assembly consists of the following four values: 45 | // 46 | // Major Version 47 | // Minor Version 48 | // Build Number 49 | // Revision 50 | // 51 | // You can specify all the values or you can default the Build and Revision Numbers 52 | // by using the '*' as shown below: 53 | // [assembly: AssemblyVersion("1.0.*")] 54 | [assembly: AssemblyVersion("1.0.0.0")] 55 | [assembly: AssemblyFileVersion("1.0.0.0")] 56 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxExample/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Resources; 3 | using System.Runtime.CompilerServices; 4 | using System.Runtime.InteropServices; 5 | using System.Windows; 6 | 7 | // General Information about an assembly is controlled through the following 8 | // set of attributes. Change these attribute values to modify the information 9 | // associated with an assembly. 10 | [assembly: AssemblyTitle("CurrencyTextBoxExample")] 11 | [assembly: AssemblyDescription("")] 12 | [assembly: AssemblyConfiguration("")] 13 | [assembly: AssemblyCompany("")] 14 | [assembly: AssemblyProduct("CurrencyTextBoxExample")] 15 | [assembly: AssemblyCopyright("")] 16 | [assembly: AssemblyTrademark("")] 17 | [assembly: AssemblyCulture("")] 18 | 19 | // Setting ComVisible to false makes the types in this assembly not visible 20 | // to COM components. If you need to access a type in this assembly from 21 | // COM, set the ComVisible attribute to true on that type. 22 | [assembly: ComVisible(false)] 23 | 24 | //In order to begin building localizable applications, set 25 | //CultureYouAreCodingWith in your .csproj file 26 | //inside a . For example, if you are using US english 27 | //in your source files, set the to en-US. Then uncomment 28 | //the NeutralResourceLanguage attribute below. Update the "en-US" in 29 | //the line below to match the UICulture setting in the project file. 30 | 31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] 32 | 33 | 34 | [assembly: ThemeInfo( 35 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located 36 | //(used if a resource is not found in the page, 37 | // or application resource dictionaries) 38 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located 39 | //(used if a resource is not found in the page, 40 | // app, or any theme specific resource dictionaries) 41 | )] 42 | 43 | 44 | // Version information for an assembly consists of the following four values: 45 | // 46 | // Major Version 47 | // Minor Version 48 | // Build Number 49 | // Revision 50 | // 51 | // You can specify all the values or you can default the Build and Revision Numbers 52 | // by using the '*' as shown below: 53 | // [assembly: AssemblyVersion("1.0.*")] 54 | [assembly: AssemblyVersion("1.0.0.0")] 55 | [assembly: AssemblyFileVersion("1.0.0.0")] 56 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxControl.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.23107.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CurrencyTextBoxControl", "CurrencyTextBoxControl\CurrencyTextBoxControl.csproj", "{144A2EE7-09EC-4618-BA94-6593205962C9}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CurrencyTextBoxExample", "CurrencyTextBoxExample\CurrencyTextBoxExample.csproj", "{13F3D236-4FC1-433F-9F2E-E0932035A5AD}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Debug|Mixed Platforms = Debug|Mixed Platforms 14 | Debug|x86 = Debug|x86 15 | Release|Any CPU = Release|Any CPU 16 | Release|Mixed Platforms = Release|Mixed Platforms 17 | Release|x86 = Release|x86 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {144A2EE7-09EC-4618-BA94-6593205962C9}.Debug|Any CPU.ActiveCfg = Release|Any CPU 21 | {144A2EE7-09EC-4618-BA94-6593205962C9}.Debug|Any CPU.Build.0 = Release|Any CPU 22 | {144A2EE7-09EC-4618-BA94-6593205962C9}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU 23 | {144A2EE7-09EC-4618-BA94-6593205962C9}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU 24 | {144A2EE7-09EC-4618-BA94-6593205962C9}.Debug|x86.ActiveCfg = Debug|Any CPU 25 | {144A2EE7-09EC-4618-BA94-6593205962C9}.Release|Any CPU.ActiveCfg = Release|Any CPU 26 | {144A2EE7-09EC-4618-BA94-6593205962C9}.Release|Any CPU.Build.0 = Release|Any CPU 27 | {144A2EE7-09EC-4618-BA94-6593205962C9}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU 28 | {144A2EE7-09EC-4618-BA94-6593205962C9}.Release|Mixed Platforms.Build.0 = Release|Any CPU 29 | {144A2EE7-09EC-4618-BA94-6593205962C9}.Release|x86.ActiveCfg = Release|Any CPU 30 | {13F3D236-4FC1-433F-9F2E-E0932035A5AD}.Debug|Any CPU.ActiveCfg = Debug|x86 31 | {13F3D236-4FC1-433F-9F2E-E0932035A5AD}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 32 | {13F3D236-4FC1-433F-9F2E-E0932035A5AD}.Debug|Mixed Platforms.Build.0 = Debug|x86 33 | {13F3D236-4FC1-433F-9F2E-E0932035A5AD}.Debug|x86.ActiveCfg = Debug|x86 34 | {13F3D236-4FC1-433F-9F2E-E0932035A5AD}.Debug|x86.Build.0 = Debug|x86 35 | {13F3D236-4FC1-433F-9F2E-E0932035A5AD}.Release|Any CPU.ActiveCfg = Release|x86 36 | {13F3D236-4FC1-433F-9F2E-E0932035A5AD}.Release|Mixed Platforms.ActiveCfg = Release|x86 37 | {13F3D236-4FC1-433F-9F2E-E0932035A5AD}.Release|Mixed Platforms.Build.0 = Release|x86 38 | {13F3D236-4FC1-433F-9F2E-E0932035A5AD}.Release|x86.ActiveCfg = Release|x86 39 | {13F3D236-4FC1-433F-9F2E-E0932035A5AD}.Release|x86.Build.0 = Release|x86 40 | EndGlobalSection 41 | GlobalSection(SolutionProperties) = preSolution 42 | HideSolutionNode = FALSE 43 | EndGlobalSection 44 | EndGlobal 45 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxExample/MainWindow.xaml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 31 | 32 | 39 | 40 | 41 | 42 | 48 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxControl/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.225 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace CurrencyTextBoxControl.Properties { 12 | using System; 13 | 14 | 15 | /// 16 | /// A strongly-typed resource class, for looking up localized strings, etc. 17 | /// 18 | // This class was auto-generated by the StronglyTypedResourceBuilder 19 | // class via a tool like ResGen or Visual Studio. 20 | // To add or remove a member, edit your .ResX file then rerun ResGen 21 | // with the /str option, or rebuild your VS project. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal Resources() { 33 | } 34 | 35 | /// 36 | /// Returns the cached ResourceManager instance used by this class. 37 | /// 38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 39 | internal static global::System.Resources.ResourceManager ResourceManager { 40 | get { 41 | if (object.ReferenceEquals(resourceMan, null)) { 42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("CurrencyTextBoxControl.Properties.Resources", typeof(Resources).Assembly); 43 | resourceMan = temp; 44 | } 45 | return resourceMan; 46 | } 47 | } 48 | 49 | /// 50 | /// Overrides the current thread's CurrentUICulture property for all 51 | /// resource lookups using this strongly typed resource class. 52 | /// 53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 54 | internal static global::System.Globalization.CultureInfo Culture { 55 | get { 56 | return resourceCulture; 57 | } 58 | set { 59 | resourceCulture = value; 60 | } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxExample/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.225 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace CurrencyTextBoxExample.Properties { 12 | using System; 13 | 14 | 15 | /// 16 | /// A strongly-typed resource class, for looking up localized strings, etc. 17 | /// 18 | // This class was auto-generated by the StronglyTypedResourceBuilder 19 | // class via a tool like ResGen or Visual Studio. 20 | // To add or remove a member, edit your .ResX file then rerun ResGen 21 | // with the /str option, or rebuild your VS project. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal Resources() { 33 | } 34 | 35 | /// 36 | /// Returns the cached ResourceManager instance used by this class. 37 | /// 38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 39 | internal static global::System.Resources.ResourceManager ResourceManager { 40 | get { 41 | if (object.ReferenceEquals(resourceMan, null)) { 42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("CurrencyTextBoxExample.Properties.Resources", typeof(Resources).Assembly); 43 | resourceMan = temp; 44 | } 45 | return resourceMan; 46 | } 47 | } 48 | 49 | /// 50 | /// Overrides the current thread's CurrentUICulture property for all 51 | /// resource lookups using this strongly typed resource class. 52 | /// 53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 54 | internal static global::System.Globalization.CultureInfo Culture { 55 | get { 56 | return resourceCulture; 57 | } 58 | set { 59 | resourceCulture = value; 60 | } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxControl/Themes/Generic.xaml: -------------------------------------------------------------------------------- 1 | 5 | 46 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxControl/CurrencyTextBoxControl.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {144A2EE7-09EC-4618-BA94-6593205962C9} 9 | library 10 | Properties 11 | CurrencyTextBoxControl 12 | CurrencyTextBoxControl 13 | v4.0 14 | 512 15 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 16 | 4 17 | 18 | SAK 19 | SAK 20 | SAK 21 | SAK 22 | 23 | 24 | true 25 | full 26 | false 27 | bin\Debug\ 28 | DEBUG;TRACE 29 | prompt 30 | 4 31 | 32 | 33 | pdbonly 34 | true 35 | bin\Release\ 36 | TRACE 37 | prompt 38 | 4 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | MSBuild:Compile 56 | Designer 57 | 58 | 59 | Code 60 | 61 | 62 | 63 | 64 | Code 65 | 66 | 67 | True 68 | True 69 | Resources.resx 70 | 71 | 72 | True 73 | Settings.settings 74 | True 75 | 76 | 77 | ResXFileCodeGenerator 78 | Resources.Designer.cs 79 | 80 | 81 | SettingsSingleFileGenerator 82 | Settings.Designer.cs 83 | 84 | 85 | 86 | 87 | 94 | -------------------------------------------------------------------------------- /.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 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | 24 | # Visual Studio 2015 cache/options directory 25 | .vs/ 26 | # Uncomment if you have tasks that create the project's static files in wwwroot 27 | #wwwroot/ 28 | 29 | # MSTest test Results 30 | [Tt]est[Rr]esult*/ 31 | [Bb]uild[Ll]og.* 32 | 33 | # NUNIT 34 | *.VisualState.xml 35 | TestResult.xml 36 | 37 | # Build Results of an ATL Project 38 | [Dd]ebugPS/ 39 | [Rr]eleasePS/ 40 | dlldata.c 41 | 42 | # DNX 43 | project.lock.json 44 | artifacts/ 45 | 46 | *_i.c 47 | *_p.c 48 | *_i.h 49 | *.ilk 50 | *.meta 51 | *.obj 52 | *.pch 53 | *.pdb 54 | *.pgc 55 | *.pgd 56 | *.rsp 57 | *.sbr 58 | *.tlb 59 | *.tli 60 | *.tlh 61 | *.tmp 62 | *.tmp_proj 63 | *.log 64 | *.vspscc 65 | *.vssscc 66 | .builds 67 | *.pidb 68 | *.svclog 69 | *.scc 70 | 71 | # Chutzpah Test files 72 | _Chutzpah* 73 | 74 | # Visual C++ cache files 75 | ipch/ 76 | *.aps 77 | *.ncb 78 | *.opendb 79 | *.opensdf 80 | *.sdf 81 | *.cachefile 82 | 83 | # Visual Studio profiler 84 | *.psess 85 | *.vsp 86 | *.vspx 87 | *.sap 88 | 89 | # TFS 2012 Local Workspace 90 | $tf/ 91 | 92 | # Guidance Automation Toolkit 93 | *.gpState 94 | 95 | # ReSharper is a .NET coding add-in 96 | _ReSharper*/ 97 | *.[Rr]e[Ss]harper 98 | *.DotSettings.user 99 | 100 | # JustCode is a .NET coding add-in 101 | .JustCode 102 | 103 | # TeamCity is a build add-in 104 | _TeamCity* 105 | 106 | # DotCover is a Code Coverage Tool 107 | *.dotCover 108 | 109 | # NCrunch 110 | _NCrunch_* 111 | .*crunch*.local.xml 112 | nCrunchTemp_* 113 | 114 | # MightyMoose 115 | *.mm.* 116 | AutoTest.Net/ 117 | 118 | # Web workbench (sass) 119 | .sass-cache/ 120 | 121 | # Installshield output folder 122 | [Ee]xpress/ 123 | 124 | # DocProject is a documentation generator add-in 125 | DocProject/buildhelp/ 126 | DocProject/Help/*.HxT 127 | DocProject/Help/*.HxC 128 | DocProject/Help/*.hhc 129 | DocProject/Help/*.hhk 130 | DocProject/Help/*.hhp 131 | DocProject/Help/Html2 132 | DocProject/Help/html 133 | 134 | # Click-Once directory 135 | publish/ 136 | 137 | # Publish Web Output 138 | *.[Pp]ublish.xml 139 | *.azurePubxml 140 | # TODO: Comment the next line if you want to checkin your web deploy settings 141 | # but database connection strings (with potential passwords) will be unencrypted 142 | *.pubxml 143 | *.publishproj 144 | 145 | # NuGet Packages 146 | *.nupkg 147 | # The packages folder can be ignored because of Package Restore 148 | **/packages/* 149 | # except build/, which is used as an MSBuild target. 150 | !**/packages/build/ 151 | # Uncomment if necessary however generally it will be regenerated when needed 152 | #!**/packages/repositories.config 153 | # NuGet v3's project.json files produces more ignoreable files 154 | *.nuget.props 155 | *.nuget.targets 156 | 157 | # Microsoft Azure Build Output 158 | csx/ 159 | *.build.csdef 160 | 161 | # Microsoft Azure Emulator 162 | ecf/ 163 | rcf/ 164 | 165 | # Microsoft Azure ApplicationInsights config file 166 | ApplicationInsights.config 167 | 168 | # Windows Store app package directory 169 | AppPackages/ 170 | BundleArtifacts/ 171 | 172 | # Visual Studio cache files 173 | # files ending in .cache can be ignored 174 | *.[Cc]ache 175 | # but keep track of directories ending in .cache 176 | !*.[Cc]ache/ 177 | 178 | # Others 179 | ClientBin/ 180 | ~$* 181 | *~ 182 | *.dbmdl 183 | *.dbproj.schemaview 184 | *.pfx 185 | *.publishsettings 186 | node_modules/ 187 | orleans.codegen.cs 188 | 189 | # RIA/Silverlight projects 190 | Generated_Code/ 191 | 192 | # Backup & report files from converting an old project file 193 | # to a newer Visual Studio version. Backup files are not needed, 194 | # because we have git ;-) 195 | _UpgradeReport_Files/ 196 | Backup*/ 197 | UpgradeLog*.XML 198 | UpgradeLog*.htm 199 | 200 | # SQL Server files 201 | *.mdf 202 | *.ldf 203 | 204 | # Business Intelligence projects 205 | *.rdl.data 206 | *.bim.layout 207 | *.bim_*.settings 208 | 209 | # Microsoft Fakes 210 | FakesAssemblies/ 211 | 212 | # GhostDoc plugin setting file 213 | *.GhostDoc.xml 214 | 215 | # Node.js Tools for Visual Studio 216 | .ntvs_analysis.dat 217 | 218 | # Visual Studio 6 build log 219 | *.plg 220 | 221 | # Visual Studio 6 workspace options file 222 | *.opt 223 | 224 | # Visual Studio LightSwitch build output 225 | **/*.HTMLClient/GeneratedArtifacts 226 | **/*.DesktopClient/GeneratedArtifacts 227 | **/*.DesktopClient/ModelManifest.xml 228 | **/*.Server/GeneratedArtifacts 229 | **/*.Server/ModelManifest.xml 230 | _Pvt_Extensions 231 | 232 | # Paket dependency manager 233 | .paket/paket.exe 234 | 235 | # FAKE - F# Make 236 | .fake/ 237 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxExample/CurrencyTextBoxExample.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | x86 6 | 8.0.30703 7 | 2.0 8 | {13F3D236-4FC1-433F-9F2E-E0932035A5AD} 9 | WinExe 10 | Properties 11 | CurrencyTextBoxExample 12 | CurrencyTextBoxExample 13 | v4.0 14 | 512 15 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 16 | 4 17 | 18 | SAK 19 | SAK 20 | SAK 21 | SAK 22 | 23 | 24 | x86 25 | true 26 | full 27 | false 28 | bin\Debug\ 29 | DEBUG;TRACE 30 | prompt 31 | 4 32 | 33 | 34 | x86 35 | pdbonly 36 | true 37 | bin\Release\ 38 | TRACE 39 | prompt 40 | 4 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | MSBuild:Compile 57 | Designer 58 | 59 | 60 | MSBuild:Compile 61 | Designer 62 | 63 | 64 | App.xaml 65 | Code 66 | 67 | 68 | MainWindow.xaml 69 | Code 70 | 71 | 72 | 73 | 74 | Code 75 | 76 | 77 | True 78 | True 79 | Resources.resx 80 | 81 | 82 | True 83 | Settings.settings 84 | True 85 | 86 | 87 | ResXFileCodeGenerator 88 | Resources.Designer.cs 89 | 90 | 91 | 92 | SettingsSingleFileGenerator 93 | Settings.Designer.cs 94 | 95 | 96 | 97 | 98 | 99 | {144A2EE7-09EC-4618-BA94-6593205962C9} 100 | CurrencyTextBoxControl 101 | 102 | 103 | 104 | 111 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxControl/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxExample/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /src/CurrencyTextBoxControl/CurrencyTextBox.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Windows; 3 | using System.Windows.Controls; 4 | using System.Windows.Data; 5 | using System.Windows.Input; 6 | 7 | namespace CurrencyTextBoxControl 8 | { 9 | public class CurrencyTextBox : TextBox 10 | { 11 | #region Dependency Properties 12 | public static readonly DependencyProperty NumberProperty = DependencyProperty.Register( 13 | "Number", 14 | typeof(decimal), 15 | typeof(CurrencyTextBox), 16 | new FrameworkPropertyMetadata(0M, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); 17 | public decimal Number 18 | { 19 | get 20 | { 21 | return (decimal)GetValue(NumberProperty); 22 | } 23 | set 24 | { 25 | SetValue(NumberProperty, value); 26 | } 27 | } 28 | 29 | public static readonly DependencyProperty StringFormatProperty = DependencyProperty.Register( 30 | "StringFormat", 31 | typeof(string), 32 | typeof(CurrencyTextBox), 33 | new FrameworkPropertyMetadata("C", StringFormatPropertyChanged)); 34 | public string StringFormat 35 | { 36 | get 37 | { 38 | return (string)GetValue(StringFormatProperty); 39 | } 40 | set 41 | { 42 | SetValue(StringFormatProperty, value); 43 | } 44 | } 45 | 46 | private static void StringFormatPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) 47 | { 48 | // Update the Text binding with the new StringFormat 49 | var textBinding = new Binding(); 50 | textBinding.Path = new PropertyPath("Number"); 51 | textBinding.RelativeSource = new RelativeSource(RelativeSourceMode.Self); 52 | textBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 53 | textBinding.StringFormat = (string)e.NewValue; 54 | 55 | BindingOperations.SetBinding(obj, TextBox.TextProperty, textBinding); 56 | } 57 | #endregion 58 | 59 | #region Constructor 60 | static CurrencyTextBox() 61 | { 62 | DefaultStyleKeyProperty.OverrideMetadata( 63 | typeof(CurrencyTextBox), 64 | new FrameworkPropertyMetadata(typeof(CurrencyTextBox))); 65 | } 66 | 67 | public override void OnApplyTemplate() 68 | { 69 | base.OnApplyTemplate(); 70 | 71 | // Bind Text to Number with the specified StringFormat 72 | var textBinding = new Binding(); 73 | textBinding.Path = new PropertyPath("Number"); 74 | textBinding.RelativeSource = new RelativeSource(RelativeSourceMode.Self); 75 | textBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 76 | textBinding.StringFormat = this.StringFormat; 77 | 78 | BindingOperations.SetBinding(this, TextBox.TextProperty, textBinding); 79 | 80 | // Disable copy/paste 81 | DataObject.AddCopyingHandler(this, PastingEventHandler); 82 | DataObject.AddPastingHandler(this, PastingEventHandler); 83 | 84 | this.CaretIndex = this.Text.Length; 85 | this.PreviewKeyDown += TextBox_PreviewKeyDown; 86 | this.PreviewMouseDown += TextBox_PreviewMouseDown; 87 | this.PreviewMouseUp += TextBox_PreviewMouseUp; 88 | this.TextChanged += TextBox_TextChanged; 89 | this.ContextMenu = null; 90 | } 91 | #endregion 92 | 93 | #region Events 94 | private void TextBox_TextChanged(object sender, TextChangedEventArgs e) 95 | { 96 | var tb = sender as TextBox; 97 | 98 | if (Number < 0 && tb.GetBindingExpression(TextBox.TextProperty).ParentBinding.StringFormat == "C") 99 | { 100 | // If a negative number and a StringFormat of "C" is used, then 101 | // place the caret before the closing paren. 102 | tb.CaretIndex = tb.Text.Length - 1; 103 | } 104 | else 105 | { 106 | // Keep the caret at the end 107 | tb.CaretIndex = tb.Text.Length; 108 | } 109 | } 110 | 111 | private void TextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e) 112 | { 113 | // Prevent changing the caret index 114 | e.Handled = true; 115 | (sender as TextBox).Focus(); 116 | } 117 | 118 | void TextBox_PreviewMouseUp(object sender, MouseButtonEventArgs e) 119 | { 120 | // Prevent changing the caret index 121 | e.Handled = true; 122 | (sender as TextBox).Focus(); 123 | } 124 | 125 | private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e) 126 | { 127 | if (IsNumericKey(e.Key)) 128 | { 129 | e.Handled = true; 130 | 131 | // Push the new number from the right 132 | if (Number < 0) 133 | { 134 | Number = (Number * 10M) - (GetDigitFromKey(e.Key) / 100M); 135 | } 136 | else 137 | { 138 | Number = (Number * 10M) + (GetDigitFromKey(e.Key) / 100M); 139 | } 140 | } 141 | else if (e.Key == Key.Back) 142 | { 143 | e.Handled = true; 144 | 145 | // Remove the right-most digit 146 | Number = (Number - (Number % 0.1M)) / 10M; 147 | } 148 | else if (e.Key == Key.Delete) 149 | { 150 | e.Handled = true; 151 | 152 | Number = 0M; 153 | } 154 | else if (e.Key == Key.Subtract || e.Key == Key.OemMinus) 155 | { 156 | e.Handled = true; 157 | 158 | Number *= -1; 159 | } 160 | else if (IsIgnoredKey(e.Key)) 161 | { 162 | e.Handled = false; 163 | } 164 | else 165 | { 166 | e.Handled = true; 167 | } 168 | } 169 | 170 | private void PastingEventHandler(object sender, DataObjectEventArgs e) 171 | { 172 | // Prevent copy/paste 173 | e.CancelCommand(); 174 | } 175 | #endregion 176 | 177 | #region Private Methods 178 | private decimal GetDigitFromKey(Key key) 179 | { 180 | switch (key) 181 | { 182 | case Key.D0: 183 | case Key.NumPad0: return 0M; 184 | case Key.D1: 185 | case Key.NumPad1: return 1M; 186 | case Key.D2: 187 | case Key.NumPad2: return 2M; 188 | case Key.D3: 189 | case Key.NumPad3: return 3M; 190 | case Key.D4: 191 | case Key.NumPad4: return 4M; 192 | case Key.D5: 193 | case Key.NumPad5: return 5M; 194 | case Key.D6: 195 | case Key.NumPad6: return 6M; 196 | case Key.D7: 197 | case Key.NumPad7: return 7M; 198 | case Key.D8: 199 | case Key.NumPad8: return 8M; 200 | case Key.D9: 201 | case Key.NumPad9: return 9M; 202 | default: throw new ArgumentOutOfRangeException("Invalid key: " + key.ToString()); 203 | } 204 | } 205 | 206 | private bool IsNumericKey(Key key) 207 | { 208 | return key == Key.D0 || 209 | key == Key.D1 || 210 | key == Key.D2 || 211 | key == Key.D3 || 212 | key == Key.D4 || 213 | key == Key.D5 || 214 | key == Key.D6 || 215 | key == Key.D7 || 216 | key == Key.D8 || 217 | key == Key.D9 || 218 | key == Key.NumPad0 || 219 | key == Key.NumPad1 || 220 | key == Key.NumPad2 || 221 | key == Key.NumPad3 || 222 | key == Key.NumPad4 || 223 | key == Key.NumPad5 || 224 | key == Key.NumPad6 || 225 | key == Key.NumPad7 || 226 | key == Key.NumPad8 || 227 | key == Key.NumPad9; 228 | } 229 | 230 | private bool IsBackspaceKey(Key key) 231 | { 232 | return key == Key.Back; 233 | } 234 | 235 | private bool IsIgnoredKey(Key key) 236 | { 237 | return key == Key.Up || 238 | key == Key.Down || 239 | key == Key.Tab || 240 | key == Key.Enter; 241 | } 242 | #endregion 243 | } 244 | } 245 | --------------------------------------------------------------------------------