質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

2回答

15072閲覧

WPFの入力値検証エラー時の赤枠表示を消したい

dekag

総合スコア8

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2016/10/20 07:09

編集2022/10/25 19:31

###前提・実現したいこと
WPFのTextBoxでIDataErrorInfoインターフェイスを使用して
入力値の検証を行い、入力値に問題がある場合に赤枠表示する機能を実装しました。
その際にTextBoxの設置されているWindowのサイズを縮小すると
対象のテキストボックスが隠れた状態でも
エラー表示の赤枠が画面の全面に浮き出てしまいます。
対象のテキストボックスが隠れた状態ではエラー表示の赤枠が表示しないようにしたいです。

###試したこと
TextBoxのスタイルのValidation.ErrorTemplateを
TextBoxのActualHeightが10以下になると赤枠のBorderを非表示にする
という形で現象の解決が見られたのですが、
レイアウトによってはTextBoxが隠れても
ActualHeightが変わらないケースもあるので
完全な解決に至ってません。
TextBoxが画面の縮小によって画面に表示されていないということを
リアルタイムで検出できればなんとかなりそうなのですが
何か良い方法はないでしょうか?

###該当のソースコード
試したこと で作成したテキストボックスのスタイル

XAML

1<Window x:Class="WPFTextBoxValidation.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:converter="clr-namespace:Emr.Themes.Converters" 5 Title="www.CodeArsenal.net" 6 Height="240" Width="350"> 7 8 <Window.Resources> 9 <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /> 10 <converter:SizeVisiblityConverter x:Key="SizeVisiblityConverter" /> 11 <Style TargetType="{x:Type Label}"> 12 <Setter Property="Margin" Value="5,0,5,0" /> 13 <Setter Property="HorizontalAlignment" Value="Left" /> 14 </Style> 15 <Style TargetType="{x:Type TextBox}"> 16 <Setter Property="MinHeight" Value="0" /> 17 <Setter Property="VerticalAlignment" Value="Center" /> 18 <Setter Property="Margin" Value="0,2,40,2" /> 19 <Setter Property="Validation.ErrorTemplate"> 20 <Setter.Value> 21 <ControlTemplate> 22 <DockPanel LastChildFill="true"> 23 <Border Background="OrangeRed" DockPanel.Dock="right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="5" 24 ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" 25 Visibility="{Binding ElementName=customAdorner, Path=AdornedElement.ActualHeight , Converter={StaticResource SizeVisiblityConverter}}"> 26 <TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white" 27 Visibility="{Binding ElementName=customAdorner, Path=AdornedElement.ActualHeight , Converter={StaticResource SizeVisiblityConverter}}" /> 28 </Border> 29 <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center"> 30 <Border BorderBrush="Red" BorderThickness="1" 31 Visibility="{Binding ElementName=customAdorner, Path=AdornedElement.ActualHeight , Converter={StaticResource SizeVisiblityConverter}}"/> 32 </AdornedElementPlaceholder> 33 </DockPanel> 34 </ControlTemplate> 35 </Setter.Value> 36 </Setter> 37 </Style> 38 </Window.Resources> 39 40 <Grid x:Name="grid_EmployeeData" Margin="0,0,0,100"> 41 <Grid.CommandBindings> 42 <CommandBinding Command="New" CanExecute="Confirm_CanExecute" Executed="Confirm_Executed" /> 43 </Grid.CommandBindings> 44 <Grid.RowDefinitions> 45 <RowDefinition Height="20"/> 46 <RowDefinition Height="20"/> 47 <RowDefinition Height="20"/> 48 <RowDefinition Height="20"/> 49 </Grid.RowDefinitions> 50 51 <TextBox x:Name="textBox_Name" Grid.Row="0" Validation.Error="Validation_Error" 52 Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=Name, 53 ValidatesOnDataErrors=true, NotifyOnValidationError=true}" /> 54 <TextBox x:Name="textBox_Position" Grid.Row="1" Validation.Error="Validation_Error" 55 Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=Position, 56 ValidatesOnDataErrors=true, NotifyOnValidationError=true}" /> 57 <TextBox x:Name="textBox_Salary" Grid.Row="2" Width="50" HorizontalAlignment="left" 58 Validation.Error="Validation_Error" MaxLength="5" 59 Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=Salary, 60 ValidatesOnDataErrors=true, NotifyOnValidationError=true}"/> 61 <Button Content="Confirm" Grid.Row="3" Margin="0,0,10,0" 62 HorizontalAlignment="right" VerticalAlignment="Center" Command="New"/> 63 </Grid> 64</Window>

###補足情報(言語/FW/ツール等のバージョンなど)
C#5.0 NetFrameWork4.5.1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

別件を検索していたらヒットしまして、有用な回答がないため回答しておきます(今更もいいところですが^^;

対象のテキストボックスが隠れた状態でも
エラー表示の赤枠が画面の全面に浮き出てしまいます。

デフォルトのAdornerLayerは、クライアントエリア全体を覆っているためそうなります。

AdornerDecoratorを追加することで、クリッピングされたAdornerLayerを作成することが可能です。
AdornerDecorator クラス (System.Windows.Documents) | Microsoft Learn

わかりやすくするため

  • 色を付けた
  • 切り替えられるようにした
  • xamlを簡略化した
  • C#コードはライブラリに全投げ

等、変更させていただきました(本題には無関係のはず)

.NET6です^^
NuGet Gallery | CommunityToolkit.Mvvm 8.0.0

xml

1<Window 2 x:Class="Questions52127.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 Width="350" 6 Height="220" 7 Background="Gainsboro"> 8 <Window.Resources> 9 <Style TargetType="{x:Type TextBox}"> 10 <Setter Property="Margin" Value="0,2,40,2" /> 11 <Setter Property="Validation.ErrorTemplate"> 12 <Setter.Value> 13 <ControlTemplate> 14 <DockPanel> 15 <Border 16 Width="20" 17 Height="20" 18 Margin="5,0,0,0" 19 Background="OrangeRed" 20 CornerRadius="5" 21 DockPanel.Dock="Right" 22 ToolTip="{Binding AdornedElement.(Validation.Errors)[0].ErrorContent, ElementName=customAdorner}"> 23 <TextBlock 24 HorizontalAlignment="Center" 25 VerticalAlignment="Center" 26 FontWeight="Bold" 27 Foreground="White" 28 Text="!" /> 29 </Border> 30 <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center"> 31 <Border BorderBrush="Red" BorderThickness="1" /> 32 </AdornedElementPlaceholder> 33 </DockPanel> 34 </ControlTemplate> 35 </Setter.Value> 36 </Setter> 37 </Style> 38 </Window.Resources> 39 40 <Grid Margin="10,10,10,100"> 41 <AdornerDecorator ClipToBounds="{Binding IsChecked, ElementName=checkBox}"> 42 <StackPanel Background="Snow"> 43 <CheckBox x:Name="checkBox" Content="ClipToBounds" /> 44 <TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" /> 45 <TextBox Text="{Binding Position, UpdateSourceTrigger=PropertyChanged}" /> 46 <TextBox 47 Width="50" 48 HorizontalAlignment="Left" 49 Text="{Binding Salary, UpdateSourceTrigger=PropertyChanged}" /> 50 </StackPanel> 51 </AdornerDecorator> 52 </Grid> 53</Window>

cs

1using System.ComponentModel.DataAnnotations; 2using System.Windows; 3using CommunityToolkit.Mvvm.ComponentModel; 4 5namespace Questions52127; 6 7 8public partial class Employee : ObservableValidator 9{ 10 [ObservableProperty] 11 [NotifyDataErrorInfo] 12 [Required] 13 private string? _Name; 14 15 16 [ObservableProperty] 17 [NotifyDataErrorInfo] 18 [Required] 19 private string? _Position; 20 21 [ObservableProperty] 22 [NotifyDataErrorInfo] 23 [MaxLength(5)] 24 private string? _Salary = "Salary"; 25 26 public Employee() => ValidateAllProperties(); // force validation 27} 28 29public partial class MainWindow : Window 30{ 31 public MainWindow() 32 { 33 InitializeComponent(); 34 DataContext = new Employee(); 35 } 36}

ClipToBounds="False"
ClipToBounds="True"

投稿2022/10/25 10:31

TN8001

総合スコア9244

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

こんにちは。

ご提示のStyleの場合だとMinHeightが設定されているのでActualHeightが20以下にはならないような気もします。

試しにMinHeightを外した場合は相対サイズの変更で赤枠は消えました。
どういうケースのときに発生するのか再現コードを提示してください。

投稿2016/10/24 16:53

Tak1wa

総合スコア4791

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

dekag

2016/10/27 00:19 編集

ご回答いただきありがとうございます。 >ご提示のStyleの場合だとMinHeightが設定されているのでActualHeightが20以下には>ならないような気もします。 ご指摘頂いた上記の件はその通りです、検証中のものを引用したためサンプルコードが不 適切でした。 サンプルコードを修正しました。 ルートパネルがDockPanelで要素をDock.Topしているような場合は Windowサイズの縮小により TextBoxのActualHeightが減少するのですが、 質問内容のサンプルコードのようにGridで行の高さが固定されている場合などは TextBoxのActualHeightが減少しません。 こういったケースでもエラーの赤枠表示を消せませんでしょうか? というのが質問の主旨でした。
Y.H.

2016/10/27 00:21

コードはコメントに記載するのではなく質問を編集して質問内に記載(追記)いただくと見やすくなります。
dekag

2016/10/27 07:47

ご指摘ありがとうございます。修正しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問