-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathObservableValidatorPage.xaml
More file actions
166 lines (152 loc) · 7.2 KB
/
ObservableValidatorPage.xaml
File metadata and controls
166 lines (152 loc) · 7.2 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<Page
x:Class="MvvmSampleUwp.Views.ObservableValidatorPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:MvvmSampleUwp.Controls"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:widgets="using:MvvmSampleUwp.Views.Widgets"
xmlns:mvvmSampleUwp="using:MvvmSampleUwp"
NavigationCacheMode="Enabled"
mc:Ignorable="d"
mvvmSampleUwp:ViewModelLocator.InitViewModel="True">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:InvokeCommandAction Command="{x:Bind ViewModel.LoadDocsCommand}" CommandParameter="ObservableValidator" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
<ScrollViewer Padding="{StaticResource DocumentationPageContentPadding}" CanContentRenderOutsideBounds="True">
<StackPanel Spacing="16">
<controls:DocumentationBlock Text="{x:Bind ViewModel.GetParagraph('ObservableValidator'), Mode=OneWay}" />
<controls:DocumentationBlock Text="{x:Bind ViewModel.GetParagraph('How it works'), Mode=OneWay}" />
<controls:DocumentationBlock Text="{x:Bind ViewModel.GetParagraph('Simple property'), Mode=OneWay}" />
<controls:InteractiveSample xml:space="preserve">
<controls:InteractiveSample.Content>
<widgets:ValidationFormWidget />
</controls:InteractiveSample.Content>
<controls:InteractiveSample.XamlCode>
<StackPanel Spacing="16">
<!-- Text forms -->
<controls:ValidationTextBox
HeaderText="Enter your first:"
PlaceholderText="First name"
PropertyName="FirstName"
Text="{x:Bind ViewModel.FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<controls:ValidationTextBox
HeaderText="Enter your last name:"
PlaceholderText="Last name"
PropertyName="LastName"
Text="{x:Bind ViewModel.LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<controls:ValidationTextBox
HeaderText="Enter your email address:"
PlaceholderText="Email"
PropertyName="Email"
Text="{x:Bind ViewModel.Email, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<controls:ValidationTextBox
HeaderText="Enter your phone number:"
PlaceholderText="Phone number"
PropertyName="PhoneNumber"
Text="{x:Bind ViewModel.PhoneNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<!-- Submit command -->
<Button Command="{x:Bind ViewModel.SubmitCommand}" Content="Submit" />
<!-- Popups -->
<Grid>
<muxc:InfoBar
x:Name="SuccessInfoBar"
Title="Success"
Message="The form was filled in correctly."
Severity="Success">
<interactivity:Interaction.Behaviors>
<interactions:EventTriggerBehavior EventName="FormSubmissionCompleted" SourceObject="{x:Bind ViewModel}">
<interactions:ChangePropertyAction
PropertyName="IsOpen"
TargetObject="{x:Bind SuccessInfoBar}"
Value="True" />
<interactions:ChangePropertyAction
PropertyName="IsOpen"
TargetObject="{x:Bind FailureInfoBar}"
Value="False" />
</interactions:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</muxc:InfoBar>
<muxc:InfoBar
x:Name="FailureInfoBar"
Title="Error"
Message="The form was filled in with some errors."
Severity="Error">
<muxc:InfoBar.ActionButton>
<Button Command="{x:Bind ViewModel.ShowErrorsCommand}" Content="Show errors" />
</muxc:InfoBar.ActionButton>
<interactivity:Interaction.Behaviors>
<interactions:EventTriggerBehavior EventName="FormSubmissionFailed" SourceObject="{x:Bind ViewModel}">
<interactions:ChangePropertyAction
PropertyName="IsOpen"
TargetObject="{x:Bind SuccessInfoBar}"
Value="False" />
<interactions:ChangePropertyAction
PropertyName="IsOpen"
TargetObject="{x:Bind FailureInfoBar}"
Value="True" />
</interactions:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</muxc:InfoBar>
</Grid>
</StackPanel>
</controls:InteractiveSample.XamlCode>
<controls:InteractiveSample.CSharpCode>
public partial class ValidationFormWidgetViewModel : ObservableValidator
{
private readonly IDialogService DialogService;
public ValidationFormWidgetViewModel(IDialogService dialogService)
{
DialogService = dialogService;
}
public event EventHandler? FormSubmissionCompleted;
public event EventHandler? FormSubmissionFailed;
[ObservableProperty]
[Required]
[MinLength(2)]
[MaxLength(100)]
private string? firstName;
[ObservableProperty]
[Required]
[MinLength(2)]
[MaxLength(100)]
private string? lastName;
[ObservableProperty]
[Required]
[EmailAddress]
private string? email;
[ObservableProperty]
[Required]
[Phone]
private string? phoneNumber;
[ICommand]
private void Submit()
{
ValidateAllProperties();
if (HasErrors)
{
FormSubmissionFailed?.Invoke(this, EventArgs.Empty);
}
else
{
FormSubmissionCompleted?.Invoke(this, EventArgs.Empty);
}
}
[ICommand]
private void ShowErrors()
{
string message = string.Join(Environment.NewLine, GetErrors().Select(e => e.ErrorMessage));
_ = DialogService.ShowMessageDialogAsync("Validation errors", message);
}
}
</controls:InteractiveSample.CSharpCode>
</controls:InteractiveSample>
<controls:DocumentationBlock Text="{x:Bind ViewModel.GetParagraph('Custom validation methods'), Mode=OneWay}" />
<controls:DocumentationBlock Text="{x:Bind ViewModel.GetParagraph('Custom validation attributes'), Mode=OneWay}" />
</StackPanel>
</ScrollViewer>
</Page>