ユーザーがフィールドよりも長い文字列を入力しようとすると、アイコン、ツールチップまたはチャットバブルをフィールドに表示しようとしています。私はこれを広範囲に捜してきましたが、私はこれが簡単な解決策を持たないように思われます。私は、入力が無効なときにフィールドの外観を変更する簡単な解決策を見つけましたが、警告を点滅させてフィールド内の正当な以前の値を保持するものはありません。これを達成するにはどうすればいいですか?フィールドトリガーの長さの検証エラーアイコン
答えて
適切な方法は、ErrorTemplate
とValidationRule
です。
まず、文字列の長さをチェックするものがある場合は、ValidationRuleが必要です。
public class StringLengthRule : ValidationRule
{
public int MaxLength { get; set; }
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (((string)value).Length > MaxLength)
return new ValidationResult(false, "Input too long!");
return new ValidationResult(true, null);
}
}
あなたが例えば、あなたのビューの結合にそれを添付することができます。 (私はスタイルとテンプレートを用意しました:
<TextBox>
<TextBox.Text>
<Binding RelativeSource="{RelativeSource AncestorType=local:MainWindow}"
UpdateSourceTrigger="PropertyChanged" <!-- or LostFocus -->
Path="Text">
<Binding.ValidationRules>
<local:StringLengthRule MaxLength="15"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
あなたのスタイル/テンプレートに応じて、これはこのようになります。この回答の下部にある)
これは、あまりにも内部エラーのために動作します。あなたはたとえば、int型にバインドする場合:
あなたはすべての入力がダイアログを閉じる前に有効であるかどうかを確認するために同じ検証を使用することができます。
public static class ValidationHelper
{
public static bool IsValid(DependencyObject obj)
{
// The dependency object is valid if it has no errors and all
// of its children (that are dependency objects) are error-free.
return !Validation.GetHasError(obj) &&
LogicalTreeHelper.GetChildren(obj)
.OfType<DependencyObject>()
.All(IsValid);
}
public static bool ShowValidHint(DependencyObject dependencyObject)
{
if (IsValid(dependencyObject)) return true;
MessageBox.Show(Strings.WarningInput, Strings.InputError, MessageBoxButton.OK, MessageBoxImage.Warning);
return false;
}
}
使用法:として
private void btnOk_Click(object sender, RoutedEventArgs e)
{
((Button)sender).Focus();
if (ValidationHelper.ShowValidHint(this))
DialogResult = true;
else
// show error
}
法的前回値を一部を保持します。これは、PropertyMetadata
のPropertyChangedCallback
またはCoerceValueCallback
、または(代わりにINotifyPropertyChanged
を使用している場合は)プロパティ設定ツールを使用するのが最適です。私は個人的にそれに反対することを勧めます。値を元に戻すと値は再び有効になり、キーボードをタイプしている人は自分が何を間違ったのか分からなくなるかもしれません。
私はこのテンプレートがどこにあるのか覚えていませんが、このような年齢の場合に使用しています。
コントロールテンプレート
<ControlTemplate x:Key="TextBoxErrorTemplate">
<StackPanel Orientation="Horizontal">
<!-- Defines TextBox outline border and the ToolTipCorner -->
<Border x:Name="border" BorderThickness="1.25" BorderBrush="#FFDC000C">
<Grid>
<Polygon x:Name="toolTipCorner" Grid.ZIndex="2" Margin="-1" Points="9,9 9,0 0,0" Fill="#FFDC000C" HorizontalAlignment="Right"
VerticalAlignment="Top" IsHitTestVisible="True"/>
<Polyline Grid.ZIndex="3" Points="10,10 0,0" Margin="-1" HorizontalAlignment="Right" StrokeThickness="1.5"
StrokeEndLineCap="Round" StrokeStartLineCap="Round" Stroke="White" VerticalAlignment="Top" IsHitTestVisible="True"/>
<AdornedElementPlaceholder x:Name="adorner"/>
</Grid>
</Border>
<!-- Defines the Popup -->
<Popup x:Name="placard" AllowsTransparency="True" PopupAnimation="Fade" Placement="Top" PlacementTarget="{Binding ElementName=toolTipCorner}" PlacementRectangle="10,-1,0,0">
<!-- Used to reposition Popup when dialog moves or resizes -->
<Popup.Style>
<Style TargetType="{x:Type Popup}">
<Style.Triggers>
<!-- Shows Popup when TextBox has focus -->
<DataTrigger Binding="{Binding ElementName=adorner, Path=AdornedElement.IsFocused}" Value="True">
<Setter Property="IsOpen" Value="True"/>
</DataTrigger>
<!-- Shows Popup when mouse hovers over ToolTipCorner -->
<DataTrigger Binding="{Binding ElementName=toolTipCorner, Path=IsMouseOver}" Value="True">
<Setter Property="IsOpen" Value="True"/>
</DataTrigger>
<!-- Hides Popup when window is no longer active -->
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=IsActive}" Value="False">
<Setter Property="IsOpen" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Popup.Style>
<Border x:Name="errorBorder"
Background="#FFDC000C"
Margin="0,0,8,8"
Opacity="1"
CornerRadius="4"
IsHitTestVisible="False"
MinHeight="24"
MaxWidth="267">
<Border.Effect>
<DropShadowEffect ShadowDepth="4"
Color="Black"
Opacity="0.6"
Direction="315"
BlurRadius="4"/>
</Border.Effect>
<TextBlock Text="{Binding ElementName=adorner, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}"
Foreground="White"
Margin="8,3,8,3"
TextWrapping="Wrap"/>
</Border>
</Popup>
</StackPanel>
</ControlTemplate>
素晴らしいアイデア、特にその部分のスタイル
<Style TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource TextBoxErrorTemplate}"/>
</Style>
私は、WPFの単純な問題に対する解決策の冗長さに常に驚いています。私だけでしょうか?私はコードのいくつかの行(10トップ)だけでJavaでこれを解決する方法を考えることができます。 MVVMとWPFはコードページです。 –
これで問題が解決するかどうか調べるのに時間がかかります。私はいくつかの作業の後に更新します。ありがとうございました。 –
@ÁrniSt.Sigurðssonだから...助けましたか? –
- 1. 正規表現検証 - 範囲の長さが最小値と未定義の長さで検証する
- 2. AngularJsは複数の長さの入力の長さを検証します
- 3. 長さの7つ以上の値を検証するために長さ
- 4. SQL Serverのストアドプロシージャのデータ長の検証
- 5. 最大長検証チェック
- 6. opencartのtinymce textareaでの文字列の長さの検証エラー
- 7. 電子メールの検証と長さチェックの適用
- 8. フィールドの正しい長さを検証するためのブートストラップバリデータ
- 9. cakephp-3フォームの長さ範囲の検証
- 10. RSpecを使ってパスワードの長さを検証する
- 11. Java Regexはユーザー名の長さを検証します
- 12. RhinoMocks/AssertWasCalled:引数の長さを検証していますか?
- 13. 証明書の冗長中間体の検証
- 14. JavaScriptフォームの検証 - 値の長さとブロックされた単語のリスト
- 15. 冗長プロペラ検証に反応する
- 16. 要素のエスケープされていない長さを検証する
- 17. 流暢な検証が長さに作用しません
- 18. 長さ検証クラスで問題が発生しました
- 19. ブラケットのエラーアイコンを削除するには
- 20. すべてのEditTextフィールドにエラーアイコンが表示されない
- 21. JavaScriptの数値データ型の長さを検証する方法は?
- 22. 古典UIのcq5のテキストフィールド長のカスタムダイアログ検証方法
- 23. 短い長さの認証トークン
- 24. n長さの直接証明シーケンス
- 25. 長所と短所:データベースエラーの検出とユーザー入力の検証との比較
- 26. 検証最小と別の場合の最大長
- 27. C#でのMP3長さの検索
- 28. horneqの検索方法キューの長さ
- 29. 文字列の長さの検索
- 30. minlength検証anglejsで入力の長さを表示する方法
:「分野での法律以前の値を保持しています」。つまずいている特定の問題(ヒント:質問に疑問符はありません)? – Sinatr
[ErrorTemplate'](https://www.codeproject.com/tips/690130/simple-validation-in-wpf)のように? –