2012-05-12 29 views
3

エラーのコンテンツを近くで表示する代わりに、エラーの1つ(IDataErrorInfo.Errors)を画面の1つの場所に表示したいと思います。そのために、フォームの最後にtextBlockを配置しました。バインディング(Validation.Errors)[0] .ErrorContentの現在のフォーカスされた要素を取得できますか。WPFでフォーカス要素を取得するXAML

これはコードビハインドではなく、XAMLで行う必要があります。フォーカスは、その後、変更されたことを、要素のエラー内容が画面のTextBlockに置か一番下に表示され

..

おかげ&よろしく Dineshbabu Sengottian

答えて

3

あなたがFocusManager.FocusedElementを使用してフォーカスのある要素にアクセスすることができます。ここで(コードビハインドテスト用IDataErrorInfoエラーを提供するのに必要なためもちろん除く)任意のコードビハインドことなく、XAMLで純粋に動作例である:

<Window x:Class="ValidationTest.MainWindow" 
     x:Name="w" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 

    <StackPanel> 
     <TextBox x:Name="txt1" Text="{Binding Path=Value1, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, Mode=TwoWay}"/> 
     <TextBox x:Name="txt2" Text="{Binding Path=Value2, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, Mode=TwoWay}"/> 
     <TextBlock Foreground="Red" Text="{Binding 
         RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, 
         Path=(FocusManager.FocusedElement).(Validation.Errors)[0].ErrorContent}"/> 
    </StackPanel> 
</Window> 

テストクラスMainWindowは、以下のコードを有する。

namespace ValidationTest 
{ 
    public partial class MainWindow : Window, IDataErrorInfo 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      DataContext = this; 

      Value1 = "a"; 
      Value2 = "b"; 
     } 

     public string Value1 { get; set; } 
     public string Value2 { get; set; } 

     #region IDataErrorInfo Members 

     public string Error 
     { 
      get { return ""; } 
     } 

     public string this[string name] 
     { 
      get 
      { 
       if (name == "Value1" && Value1 == "x") 
       { 
        return "Value 1 must not be x"; 
       } 
       else if (name == "Value2" && Value2 == "y") 
       { 
        return "Value 2 must not be y"; 
       } 
       return ""; 
      } 
     } 

     #endregion 
    } 
} 

最初のテキストボックスに "x"を入力するか、2番目のテキストボックスに "y"を入力すると、検証エラーが発生します。

現在フォーカスされているテキストボックスのエラーメッセージは、TextBlockの2つのテキストボックスの下に表示されます。

この解決策には欠点が1つあります。デバッガの下でサンプルを実行する場合は、それらの結合のエラーが表示されます。

System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'ValidationError') from '(Validation.Errors)' (type 'ReadOnlyObservableCollection`1'). BindingExpression:Path=(0).(1)[0].ErrorContent; DataItem='MainWindow' (Name='w'); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

Validation.Errors配列が空であるため、[0]は違法であるため、現在フォーカスのある要素は、検証エラーをしていないときに、これらのデバッグエラーメッセージが発生します。

これらのエラーメッセージを無視するか(サンプルはまだ正常に動作しています)、それにもかかわらずコードビハインドが必要です。 IInputElementFocusManager.FocusedElementから返された文字列に変換するコンバータ。

+0

こんにちは私はTextBoxのためのControlTemplateを持っていたので、TextBoxのみそれは働いていません。私はTextBoxのControlTemplateには何もありません。 – dinesh

関連する問題