-
Notifications
You must be signed in to change notification settings - Fork 396
/
NumericValidationBehaviorPage.xaml
47 lines (40 loc) · 3.03 KB
/
NumericValidationBehaviorPage.xaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?xml version="1.0" encoding="UTF-8"?>
<pages:BasePage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:pages="clr-namespace:CommunityToolkit.Maui.Sample.Pages"
x:Class="CommunityToolkit.Maui.Sample.Pages.Behaviors.NumericValidationBehaviorPage"
xmlns:vm="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Behaviors"
x:TypeArguments="vm:NumericValidationBehaviorViewModel"
x:DataType="vm:NumericValidationBehaviorViewModel">
<pages:BasePage.Resources>
<mct:IsNotNullConverter x:Key="IsNotNullConverter" />
<Style x:Key="InvalidEntryStyle" TargetType="Entry">
<Setter Property="TextColor" Value="Red" />
</Style>
<Style x:Key="ValidEntryStyle"
TargetType="Entry">
<Setter Property="TextColor" Value="Green"/>
</Style>
</pages:BasePage.Resources>
<VerticalStackLayout Padding="{StaticResource ContentPadding}"
Spacing="24"
VerticalOptions="StartAndExpand">
<Label Text="The NumericValidationBehavior is a behavior that allows the user to determine if text input is a valid numeric value. For example, an Entry control can be styled differently depending on whether a valid or an invalid numeric input is provided. Additional properties handling validation are inherited from ValidationBehavior"
TextColor="{StaticResource NormalLabelTextColor}" />
<Label Text="Text color will be changed according to the configured style in case of an invalid numeric format or when the numeric value is outside of the configured boundaries (min: 1.0, max: 100.0). Maximum decimal places value equals 2." />
<Entry x:Name="SafeEntry" Placeholder="Number" Keyboard="Numeric">
<Entry.Behaviors>
<mct:NumericValidationBehavior InvalidStyle="{StaticResource InvalidEntryStyle}"
ValidStyle="{StaticResource ValidEntryStyle}"
Flags="ValidateOnValueChanged"
MinimumValue="1.0"
MaximumValue="100.0"
MaximumDecimalPlaces="2"/>
</Entry.Behaviors>
</Entry>
<Button Clicked="SetEntryValue" Text="Set Entry Value To Null" IsEnabled="{Binding Path=Text, Source={x:Reference SafeEntry}, Converter={StaticResource IsNotNullConverter}}"/>
<Label Text="After entering text then setting the above Entry value to null, the app will crash because null is not a valid input for NumericValidationBehavior and CommunityToolkit.Maui.Options.SetShouldSuppressExceptionsInBehaviors is not set to true."
TextColor="{StaticResource NormalLabelTextColor}" />
</VerticalStackLayout>
</pages:BasePage>