Created
April 23, 2017 18:45
-
-
Save MrAntix/f1fad919e06fc812e374c07c70a748ec to your computer and use it in GitHub Desktop.
Validating Number Input for a UWP XAML TextBox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MyApp | |
{ | |
public sealed class Int | |
{ | |
public static readonly DependencyProperty AttachedProperty = DependencyProperty.RegisterAttached( | |
"IntAttached", typeof(bool), typeof(Int), null); | |
public static readonly DependencyProperty ValueProperty = DependencyProperty.RegisterAttached( | |
"Value", typeof(int), typeof(Int), new PropertyMetadata(null, OnAttach)); | |
public static readonly DependencyProperty ValidateProperty = DependencyProperty.RegisterAttached( | |
"Validate", typeof(Func<int, bool>), typeof(Int), null); | |
static void OnAttach(DependencyObject sender, DependencyPropertyChangedEventArgs e) | |
{ | |
var textbox = sender as TextBox; | |
if (textbox == null || (bool) textbox.GetValue(AttachedProperty)) return; | |
textbox.SetValue(AttachedProperty, true); | |
textbox.InputScope = new InputScope(); | |
textbox.InputScope.Names.Add( | |
new InputScopeName(InputScopeNameValue.Number) | |
); | |
Brush _background = null; | |
textbox.Loaded += (ls, le) => | |
{ | |
var validate = GetValidate(textbox); | |
textbox.TextChanging += (stc, etc) => | |
{ | |
if (_background == null) | |
_background = textbox.Background; | |
if (int.TryParse(textbox.Text, out int value) | |
&& (validate == null || validate(value))) | |
{ | |
SetValue(textbox, value); | |
textbox.Background = _background; | |
} | |
else | |
{ | |
textbox.Background = new SolidColorBrush(Colors.LightPink); | |
} | |
}; | |
}; | |
} | |
public static Func<int, bool> GetValidate(DependencyObject dependencyObject) | |
{ | |
return (Func<int, bool>) (dependencyObject.GetValue(ValidateProperty) ?? int.MinValue); | |
} | |
public static void SetValidate(DependencyObject sender, Func<int, bool> value) | |
{ | |
sender.SetValue(ValidateProperty, value); | |
} | |
public static int GetValue(DependencyObject dependencyObject) | |
{ | |
return (int) (dependencyObject.GetValue(ValueProperty) ?? default(int)); | |
} | |
public static void SetValue(DependencyObject sender, int value) | |
{ | |
sender.SetValue(ValueProperty, value); | |
((TextBox) sender).Text = value.ToString(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<UserControl | |
x:Class="MyApp.UsageView" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
xmlns:local="using:MyApp" | |
mc:Ignorable="d" | |
d:DesignHeight="300" | |
d:DesignWidth="400"> | |
<Grid> | |
<TextBox | |
local:Int.Value="{x:Bind Model.Width, Mode=TwoWay}" | |
local:Int.Validate="{x:Bind ValidateWidth, Mode=OneWay}"/> | |
</Grid> | |
</UserControl> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MyApp | |
{ | |
public sealed partial class UsageView : UserControl | |
{ | |
public static readonly DependencyProperty CropProperty | |
= DependencyProperty.Register("Model", typeof(MyModel), typeof(UsageView), | |
new PropertyMetadata(default(MyModel))); | |
public CropView() | |
{ | |
InitializeComponent(); | |
} | |
public Func<int, bool> ValidateWidth | |
{ | |
get | |
{ | |
return value => value > 0 | |
&& value < 1000; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment