回答編集履歴
2
ThemeMode
test
CHANGED
@@ -125,7 +125,8 @@
|
|
125
125
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
126
126
|
xmlns:local="clr-namespace:Qo1g4oys53jcubr"
|
127
127
|
Width="525"
|
128
|
-
Height="350"
|
128
|
+
Height="350"
|
129
|
+
ThemeMode="System">
|
129
130
|
<Window.DataContext>
|
130
131
|
<local:MainWindowViewModel />
|
131
132
|
</Window.DataContext>
|
1
真面目に実装
test
CHANGED
@@ -104,5 +104,108 @@
|
|
104
104
|
[MVVM Toolkit の概要 - Community Toolkits for .NET | Microsoft Learn](https://learn.microsoft.com/ja-jp/dotnet/communitytoolkit/mvvm/)
|
105
105
|
|
106
106
|
|
107
|
-
|
108
|
-
|
107
|
+
---
|
108
|
+
|
109
|
+
> 年齢を入力するTextBoxの下にエラーメッセージ表示用のLabelをVisibility="Collapsed"にしてエラーが発生したらVisibility="Visible"にすればうまくいく
|
110
|
+
|
111
|
+
をxamlで愚直に実装しました。
|
112
|
+
`TextBox`分用意しなくてはならず`ValidationAdornerSite`の設定も面倒なので、1,2個までかなぁという気はします(`TextBox`の`Template`をいじるよりはマシかw
|
113
|
+
|
114
|
+
[Validation.ValidationAdornerSite 添付プロパティ (System.Windows.Controls) | Microsoft Learn](https://learn.microsoft.com/ja-jp/dotnet/api/system.windows.controls.validation.validationadornersite)
|
115
|
+
[Validation.ValidationAdornerSiteFor 添付プロパティ (System.Windows.Controls) | Microsoft Learn](https://learn.microsoft.com/ja-jp/dotnet/api/system.windows.controls.validation.validationadornersitefor)
|
116
|
+
[Nine Works 例外を利用したデータ検証](http://nineworks2.blog.fc2.com/blog-entry-19.html)
|
117
|
+
|
118
|
+
厳密にやるなら複数エラーにも対応すべきでしょう。
|
119
|
+
[WPFでの入力値検証・その6 ~複数のエラー表示への対応~ - SourceChord](https://sourcechord.hatenablog.com/entry/2014/10/20/004439)
|
120
|
+
|
121
|
+
```xml
|
122
|
+
<Window
|
123
|
+
x:Class="Qo1g4oys53jcubr.MainWindow"
|
124
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
125
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
126
|
+
xmlns:local="clr-namespace:Qo1g4oys53jcubr"
|
127
|
+
Width="525"
|
128
|
+
Height="350">
|
129
|
+
<Window.DataContext>
|
130
|
+
<local:MainWindowViewModel />
|
131
|
+
</Window.DataContext>
|
132
|
+
<Window.Resources>
|
133
|
+
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
|
134
|
+
|
135
|
+
<Style x:Key="ErrorStyle" TargetType="{x:Type ItemsControl}">
|
136
|
+
<Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
|
137
|
+
<Setter Property="ItemsSource" Value="{Binding (Validation.ValidationAdornerSiteFor).(Validation.Errors), RelativeSource={RelativeSource Self}}" />
|
138
|
+
<Setter Property="Visibility" Value="{Binding (Validation.ValidationAdornerSiteFor).(Validation.HasError), Converter={StaticResource BooleanToVisibilityConverter}, RelativeSource={RelativeSource Self}}" />
|
139
|
+
<Setter Property="ItemTemplate">
|
140
|
+
<Setter.Value>
|
141
|
+
<DataTemplate>
|
142
|
+
<BulletDecorator TextBlock.Foreground="Red">
|
143
|
+
<BulletDecorator.Bullet>
|
144
|
+
<TextBlock Text="! " />
|
145
|
+
</BulletDecorator.Bullet>
|
146
|
+
<TextBlock Text="{Binding ErrorContent}" TextWrapping="Wrap" />
|
147
|
+
</BulletDecorator>
|
148
|
+
</DataTemplate>
|
149
|
+
</Setter.Value>
|
150
|
+
</Setter>
|
151
|
+
</Style>
|
152
|
+
</Window.Resources>
|
153
|
+
|
154
|
+
<Grid>
|
155
|
+
<Grid.ColumnDefinitions>
|
156
|
+
<ColumnDefinition Width="Auto" />
|
157
|
+
<ColumnDefinition />
|
158
|
+
</Grid.ColumnDefinitions>
|
159
|
+
<Grid.RowDefinitions>
|
160
|
+
<RowDefinition Height="auto" />
|
161
|
+
<RowDefinition Height="auto" />
|
162
|
+
</Grid.RowDefinitions>
|
163
|
+
|
164
|
+
<Label Content="年齢" />
|
165
|
+
<StackPanel Grid.Column="1">
|
166
|
+
<TextBox
|
167
|
+
Text="{Binding InputString, UpdateSourceTrigger=PropertyChanged}"
|
168
|
+
Validation.ErrorTemplate="{x:Null}"
|
169
|
+
Validation.ValidationAdornerSite="{Binding ElementName=inputStringError}" />
|
170
|
+
<ItemsControl x:Name="inputStringError" Style="{StaticResource ErrorStyle}" />
|
171
|
+
</StackPanel>
|
172
|
+
|
173
|
+
<Label Grid.Row="1" Content="出身地" />
|
174
|
+
<StackPanel Grid.Row="1" Grid.Column="1">
|
175
|
+
<TextBox
|
176
|
+
Text="{Binding Area, UpdateSourceTrigger=PropertyChanged}"
|
177
|
+
Validation.ErrorTemplate="{x:Null}"
|
178
|
+
Validation.ValidationAdornerSite="{Binding ElementName=areaError}" />
|
179
|
+
<ItemsControl x:Name="areaError" Style="{StaticResource ErrorStyle}" />
|
180
|
+
</StackPanel>
|
181
|
+
</Grid>
|
182
|
+
</Window>
|
183
|
+
```
|
184
|
+
```cs
|
185
|
+
using System.ComponentModel.DataAnnotations;
|
186
|
+
using System.Windows;
|
187
|
+
using CommunityToolkit.Mvvm.ComponentModel;
|
188
|
+
|
189
|
+
namespace Qo1g4oys53jcubr;
|
190
|
+
|
191
|
+
public partial class MainWindow : Window
|
192
|
+
{
|
193
|
+
public MainWindow() => InitializeComponent();
|
194
|
+
}
|
195
|
+
|
196
|
+
public partial class MainWindowViewModel : ObservableValidator
|
197
|
+
{
|
198
|
+
[ObservableProperty, NotifyDataErrorInfo]
|
199
|
+
[Required(ErrorMessage = "何か入力してください")]
|
200
|
+
[StringLength(10, ErrorMessage = "10文字以内で入力してください\naaaaaa")]
|
201
|
+
[RegularExpression("[a-z]+", ErrorMessage = "a-zの文字列を入力してください")]
|
202
|
+
private string? inputString;
|
203
|
+
|
204
|
+
[ObservableProperty, NotifyDataErrorInfo]
|
205
|
+
[Required(ErrorMessage = "何か入力してください")]
|
206
|
+
private string? area;
|
207
|
+
}
|
208
|
+
```
|
209
|
+

|
210
|
+
|
211
|
+
|