|
2 | 2 | x:Class="MvvmSampleUwp.Views.ObservableValidatorPage"
|
3 | 3 | xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
4 | 4 | xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
| 5 | + xmlns:controls="using:MvvmSampleUwp.Controls" |
| 6 | + xmlns:core="using:Microsoft.Xaml.Interactions.Core" |
5 | 7 | xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
| 8 | + xmlns:interactivity="using:Microsoft.Xaml.Interactivity" |
6 | 9 | xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
7 | 10 | xmlns:widgets="using:MvvmSampleUwp.Views.Widgets"
|
8 | 11 | NavigationCacheMode="Enabled"
|
9 | 12 | mc:Ignorable="d">
|
| 13 | + <interactivity:Interaction.Behaviors> |
| 14 | + <core:EventTriggerBehavior EventName="Loaded"> |
| 15 | + <core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="ObservableValidator" /> |
| 16 | + </core:EventTriggerBehavior> |
| 17 | + </interactivity:Interaction.Behaviors> |
10 | 18 |
|
11 | 19 | <ScrollViewer Padding="{StaticResource DocumentationPageContentPadding}" CanContentRenderOutsideBounds="True">
|
12 |
| - <widgets:ValidationFormWidget DataContext="{x:Bind ViewModel.Form}" /> |
| 20 | + <StackPanel Spacing="16"> |
| 21 | + <controls:DocumentationBlock Text="{x:Bind ViewModel.GetParagraph('ObservableValidator'), Mode=OneWay}" /> |
| 22 | + <controls:DocumentationBlock Text="{x:Bind ViewModel.GetParagraph('How it works'), Mode=OneWay}" /> |
| 23 | + <controls:DocumentationBlock Text="{x:Bind ViewModel.GetParagraph('Simple property'), Mode=OneWay}" /> |
| 24 | + <controls:InteractiveSample xml:space="preserve"> |
| 25 | + <controls:InteractiveSample.Content> |
| 26 | + <widgets:ValidationFormWidget DataContext="{x:Bind ViewModel.Form}" /> |
| 27 | + </controls:InteractiveSample.Content> |
| 28 | + <controls:InteractiveSample.XamlCode> |
| 29 | +<StackPanel Spacing="16"> |
| 30 | + |
| 31 | + <!-- Text forms --> |
| 32 | + <controls:ValidationTextBox |
| 33 | + HeaderText="Enter your first:" |
| 34 | + PlaceholderText="First name" |
| 35 | + PropertyName="FirstName" |
| 36 | + Text="{x:Bind ViewModel.FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> |
| 37 | + <controls:ValidationTextBox |
| 38 | + HeaderText="Enter your last name:" |
| 39 | + PlaceholderText="Last name" |
| 40 | + PropertyName="LastName" |
| 41 | + Text="{x:Bind ViewModel.LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> |
| 42 | + <controls:ValidationTextBox |
| 43 | + HeaderText="Enter your email address:" |
| 44 | + PlaceholderText="Email" |
| 45 | + PropertyName="Email" |
| 46 | + Text="{x:Bind ViewModel.Email, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> |
| 47 | + <controls:ValidationTextBox |
| 48 | + HeaderText="Enter your phone number:" |
| 49 | + PlaceholderText="Phone number" |
| 50 | + PropertyName="PhoneNumber" |
| 51 | + Text="{x:Bind ViewModel.PhoneNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> |
| 52 | + |
| 53 | + <!-- Submit command --> |
| 54 | + <Button Command="{x:Bind ViewModel.SubmitCommand}" Content="Submit" /> |
| 55 | + |
| 56 | + <!-- Popups --> |
| 57 | + <Grid> |
| 58 | + <muxc:InfoBar |
| 59 | + x:Name="SuccessInfoBar" |
| 60 | + Title="Success" |
| 61 | + Message="The form was filled in correctly." |
| 62 | + Severity="Success"> |
| 63 | + <interactivity:Interaction.Behaviors> |
| 64 | + <interactions:EventTriggerBehavior EventName="FormSubmissionCompleted" SourceObject="{x:Bind ViewModel}"> |
| 65 | + <interactions:ChangePropertyAction |
| 66 | + PropertyName="IsOpen" |
| 67 | + TargetObject="{x:Bind SuccessInfoBar}" |
| 68 | + Value="True" /> |
| 69 | + <interactions:ChangePropertyAction |
| 70 | + PropertyName="IsOpen" |
| 71 | + TargetObject="{x:Bind FailureInfoBar}" |
| 72 | + Value="False" /> |
| 73 | + </interactions:EventTriggerBehavior> |
| 74 | + </interactivity:Interaction.Behaviors> |
| 75 | + </muxc:InfoBar> |
| 76 | + <muxc:InfoBar |
| 77 | + x:Name="FailureInfoBar" |
| 78 | + Title="Error" |
| 79 | + Message="The form was filled in with some errors." |
| 80 | + Severity="Error"> |
| 81 | + <muxc:InfoBar.ActionButton> |
| 82 | + <Button Command="{x:Bind ViewModel.ShowErrorsCommand}" Content="Show errors" /> |
| 83 | + </muxc:InfoBar.ActionButton> |
| 84 | + <interactivity:Interaction.Behaviors> |
| 85 | + <interactions:EventTriggerBehavior EventName="FormSubmissionFailed" SourceObject="{x:Bind ViewModel}"> |
| 86 | + <interactions:ChangePropertyAction |
| 87 | + PropertyName="IsOpen" |
| 88 | + TargetObject="{x:Bind SuccessInfoBar}" |
| 89 | + Value="False" /> |
| 90 | + <interactions:ChangePropertyAction |
| 91 | + PropertyName="IsOpen" |
| 92 | + TargetObject="{x:Bind FailureInfoBar}" |
| 93 | + Value="True" /> |
| 94 | + </interactions:EventTriggerBehavior> |
| 95 | + </interactivity:Interaction.Behaviors> |
| 96 | + </muxc:InfoBar> |
| 97 | + </Grid> |
| 98 | +</StackPanel> |
| 99 | + </controls:InteractiveSample.XamlCode> |
| 100 | + <controls:InteractiveSample.CSharpCode> |
| 101 | +public partial class ObservableForm : ObservableValidator |
| 102 | +{ |
| 103 | + private readonly IDialogService DialogService; |
| 104 | + |
| 105 | + public ObservableForm(IDialogService dialogService) |
| 106 | + { |
| 107 | + DialogService = dialogService; |
| 108 | + } |
| 109 | + |
| 110 | + public event EventHandler? FormSubmissionCompleted; |
| 111 | + public event EventHandler? FormSubmissionFailed; |
| 112 | + |
| 113 | + [ObservableProperty] |
| 114 | + [Required] |
| 115 | + [MinLength(2)] |
| 116 | + [MaxLength(100)] |
| 117 | + private string? firstName; |
| 118 | + |
| 119 | + [ObservableProperty] |
| 120 | + [Required] |
| 121 | + [MinLength(2)] |
| 122 | + [MaxLength(100)] |
| 123 | + private string? lastName; |
| 124 | + |
| 125 | + [ObservableProperty] |
| 126 | + [Required] |
| 127 | + [EmailAddress] |
| 128 | + private string? email; |
| 129 | + |
| 130 | + [ObservableProperty] |
| 131 | + [Required] |
| 132 | + [Phone] |
| 133 | + private string? phoneNumber; |
| 134 | + |
| 135 | + [ICommand] |
| 136 | + private void Submit() |
| 137 | + { |
| 138 | + ValidateAllProperties(); |
| 139 | + |
| 140 | + if (HasErrors) |
| 141 | + { |
| 142 | + FormSubmissionFailed?.Invoke(this, EventArgs.Empty); |
| 143 | + } |
| 144 | + else |
| 145 | + { |
| 146 | + FormSubmissionCompleted?.Invoke(this, EventArgs.Empty); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + [ICommand] |
| 151 | + private void ShowErrors() |
| 152 | + { |
| 153 | + string message = string.Join(Environment.NewLine, GetErrors().Select(e => e.ErrorMessage)); |
| 154 | + |
| 155 | + _ = DialogService.ShowMessageDialogAsync("Validation errors", message); |
| 156 | + } |
| 157 | +} |
| 158 | + </controls:InteractiveSample.CSharpCode> |
| 159 | + </controls:InteractiveSample> |
| 160 | + <controls:DocumentationBlock Text="{x:Bind ViewModel.GetParagraph('Custom validation methods'), Mode=OneWay}" /> |
| 161 | + <controls:DocumentationBlock Text="{x:Bind ViewModel.GetParagraph('Custom validation attributes'), Mode=OneWay}" /> |
| 162 | + </StackPanel> |
13 | 163 | </ScrollViewer>
|
14 | 164 | </Page>
|
0 commit comments